blob: 6c9e40b0668d26b970932894ee670bc41e5d69ad [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 Prantla83cc8a2014-02-11 21:22:59 +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 Blaikief645f962014-01-09 03:23:41 +000058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DICompileUnit CUNode,
Eric Christopher4287a492013-12-09 23:57:44 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000060 : DwarfUnit(UID, D, CUNode, A, DW, DWU) {}
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();
66}
67
68/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
69/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000070DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000071 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
72 return Value;
73}
74
Bill Wendling3495f9b2012-12-06 07:55:19 +000075/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000076/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000077int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000078 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000079 default:
80 break;
81
82 case dwarf::DW_LANG_C89:
83 case dwarf::DW_LANG_C99:
84 case dwarf::DW_LANG_C:
85 case dwarf::DW_LANG_C_plus_plus:
86 case dwarf::DW_LANG_ObjC:
87 case dwarf::DW_LANG_ObjC_plus_plus:
88 return 0;
89
90 case dwarf::DW_LANG_Fortran77:
91 case dwarf::DW_LANG_Fortran90:
92 case dwarf::DW_LANG_Fortran95:
93 return 1;
94
95 // The languages below have valid values only if the DWARF version >= 4.
96 case dwarf::DW_LANG_Java:
97 case dwarf::DW_LANG_Python:
98 case dwarf::DW_LANG_UPC:
99 case dwarf::DW_LANG_D:
100 if (dwarf::DWARF_VERSION >= 4)
101 return 0;
102 break;
103
104 case dwarf::DW_LANG_Ada83:
105 case dwarf::DW_LANG_Ada95:
106 case dwarf::DW_LANG_Cobol74:
107 case dwarf::DW_LANG_Cobol85:
108 case dwarf::DW_LANG_Modula2:
109 case dwarf::DW_LANG_Pascal83:
110 case dwarf::DW_LANG_PLI:
111 if (dwarf::DWARF_VERSION >= 4)
112 return 1;
113 break;
114 }
115
116 return -1;
117}
118
Manman Ren4dbdc902013-10-31 17:54:35 +0000119/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000120static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000121 // When the MDNode can be part of the type system, the DIE can be shared
122 // across CUs.
123 // Combining type units and cross-CU DIE sharing is lower value (since
124 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
125 // level already) but may be implementable for some value in projects
126 // building multiple independent libraries with LTO and then linking those
127 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000128 return (D.isType() ||
129 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000130 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000131}
132
133/// getDIE - Returns the debug information entry map slot for the
134/// specified debug variable. We delegate the request to DwarfDebug
135/// when the DIE for this MDNode can be shared across CUs. The mappings
136/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000137DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000138 if (isShareableAcrossCUs(D))
139 return DD->getDIE(D);
140 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000141}
142
143/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
144/// when the DIE for this MDNode can be shared across CUs. The mappings
145/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000146void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000147 if (isShareableAcrossCUs(Desc)) {
148 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000149 return;
150 }
David Blaikie2ad00162013-11-15 23:09:13 +0000151 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000152}
153
Eric Christopherbb69a272012-08-24 01:14:27 +0000154/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000155void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000156 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000157 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000158 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000159 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000160}
161
Devang Patel0e821f42011-04-12 23:21:44 +0000162/// addUInt - Add an unsigned integer attribute data and value.
163///
Eric Christophera5a79422013-12-09 23:32:48 +0000164void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
165 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000166 if (!Form)
167 Form = DIEInteger::BestForm(false, Integer);
168 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
169 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000170 Die->addValue(Attribute, *Form, Value);
171}
172
Eric Christophera5a79422013-12-09 23:32:48 +0000173void DwarfUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000174 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000175}
176
177/// addSInt - Add an signed integer attribute data and value.
178///
Eric Christophera5a79422013-12-09 23:32:48 +0000179void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
180 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000181 if (!Form)
182 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000183 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000184 Die->addValue(Attribute, *Form, Value);
185}
186
Eric Christophera5a79422013-12-09 23:32:48 +0000187void DwarfUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
188 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000189 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000190}
191
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000192/// addString - Add a string attribute data and value. We always emit a
193/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000194/// more predictable sizes. In the case of split dwarf we emit an index
195/// into another table which gets us the static offset into the string
196/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000197void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
198 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000199
200 if (!DD->useSplitDwarf())
201 return addLocalString(Die, Attribute, String);
202
203 unsigned idx = DU->getStringPoolIndex(String);
204 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000205 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000206 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000207}
208
209/// addLocalString - Add a string attribute data and value. This is guaranteed
210/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000211void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
212 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000213 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000214 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000215 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000216 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000217 else {
Eric Christophere698f532012-12-20 21:58:36 +0000218 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000219 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000220 }
Eric Christopher05893f42013-12-30 18:32:31 +0000221 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
222 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000223}
224
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000225/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000226///
Eric Christophera5a79422013-12-09 23:32:48 +0000227void DwarfUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000228 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000229 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000230}
231
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000232/// addLabel - Add a Dwarf label attribute data and value.
233///
Eric Christophera5a79422013-12-09 23:32:48 +0000234void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
235 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000236 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
237 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000238}
239
Eric Christophera5a79422013-12-09 23:32:48 +0000240void DwarfUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
241 const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000242 addLabel(Die, (dwarf::Attribute)0, Form, Label);
243}
244
Eric Christopher33ff6972013-11-21 23:46:41 +0000245/// addSectionLabel - Add a Dwarf section label attribute data and value.
246///
Eric Christophera5a79422013-12-09 23:32:48 +0000247void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
248 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000249 if (DD->getDwarfVersion() >= 4)
250 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
251 else
252 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
253}
254
255/// addSectionOffset - Add an offset into a section attribute data and value.
256///
Eric Christophera5a79422013-12-09 23:32:48 +0000257void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
258 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000259 if (DD->getDwarfVersion() >= 4)
260 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
261 else
262 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
263}
264
Eric Christopher962c9082013-01-15 23:56:56 +0000265/// addLabelAddress - Add a dwarf label attribute data and value using
266/// DW_FORM_addr or DW_FORM_GNU_addr_index.
267///
Eric Christopher4287a492013-12-09 23:57:44 +0000268void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
269 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000270 if (Label)
271 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000272
Eric Christophercf48ade2014-01-24 11:52:53 +0000273 if (!DD->useSplitDwarf()) {
274 if (Label) {
275 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
276 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
277 } else {
278 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
279 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
280 }
Eric Christopher962c9082013-01-15 23:56:56 +0000281 } else {
Eric Christophercf48ade2014-01-24 11:52:53 +0000282 unsigned idx = DU->getAddrPoolIndex(Label);
283 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
284 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000285 }
286}
287
Eric Christophere9ec2452013-01-18 22:11:33 +0000288/// addOpAddress - Add a dwarf op address data and value using the
289/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
290///
Eric Christophera5a79422013-12-09 23:32:48 +0000291void DwarfUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000292 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000293 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
294 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000295 } else {
David Blaikief2443192013-10-21 17:28:37 +0000296 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
297 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000298 }
299}
300
Eric Christopher33ff6972013-11-21 23:46:41 +0000301/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000302///
Eric Christophera5a79422013-12-09 23:32:48 +0000303void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
304 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000305 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000306 if (DD->getDwarfVersion() >= 4)
307 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
308 else
309 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000310}
311
312/// addDIEEntry - Add a DIE attribute data and value.
313///
Eric Christophera5a79422013-12-09 23:32:48 +0000314void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000315 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
316}
317
David Blaikie47f615e2013-12-17 23:32:35 +0000318void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
319 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
320 new (DIEValueAllocator) DIETypeSignature(Type));
321}
322
Eric Christophera5a79422013-12-09 23:32:48 +0000323void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
324 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000325 const DIE *DieCU = Die->getUnitOrNull();
326 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000327 if (!DieCU)
328 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000329 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000330 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000331 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000332 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
333 : dwarf::DW_FORM_ref_addr,
334 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000335}
336
Manman Renb987e512013-10-29 00:53:03 +0000337/// Create a DIE with the given Tag, add the DIE to its parent, and
338/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000339DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000340 DIE *Die = new DIE(Tag);
341 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000342 if (N)
343 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000344 return Die;
345}
346
Devang Patel0e821f42011-04-12 23:21:44 +0000347/// addBlock - Add block data.
348///
Eric Christophera5a79422013-12-09 23:32:48 +0000349void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
350 DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000351 Block->ComputeSize(Asm);
352 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
353 Die->addValue(Attribute, Block->BestForm(), Block);
354}
355
356/// addSourceLine - Add location information to specified debug information
357/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000358void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0e821f42011-04-12 23:21:44 +0000359 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000360 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000361 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000362
Devang Patel0e821f42011-04-12 23:21:44 +0000363 unsigned Line = V.getLineNumber();
364 if (Line == 0)
365 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000366 unsigned FileID =
367 DD->getOrCreateSourceID(V.getContext().getFilename(),
368 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000369 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000370 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
371 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000372}
373
374/// addSourceLine - Add location information to specified debug information
375/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000376void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0e821f42011-04-12 23:21:44 +0000377 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000378 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000379 return;
380
381 unsigned Line = G.getLineNumber();
382 if (Line == 0)
383 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000384 unsigned FileID =
385 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000386 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000387 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
388 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000389}
390
391/// addSourceLine - Add location information to specified debug information
392/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000393void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0e821f42011-04-12 23:21:44 +0000394 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000395 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000396 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000397
Devang Patel0e821f42011-04-12 23:21:44 +0000398 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000399 unsigned Line = SP.getLineNumber();
400 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000401 return;
402
Eric Christopherc2697f82013-10-19 01:04:47 +0000403 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
404 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000405 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000406 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
407 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
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, DIType Ty) {
Devang Patel0e821f42011-04-12 23:21:44 +0000413 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000414 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000415 return;
416
417 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000418 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000419 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000420 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
421 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000422 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000423 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
424 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000425}
426
427/// addSourceLine - Add location information to specified debug information
428/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000429void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
Eric Christopher70e1bd82012-03-29 08:42:56 +0000430 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000431 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000432 return;
433
434 unsigned Line = Ty.getLineNumber();
435 if (Line == 0)
436 return;
437 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000438 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000439 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000440 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000441 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
442 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000443}
444
445/// addSourceLine - Add location information to specified debug information
446/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000447void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0e821f42011-04-12 23:21:44 +0000448 // Verify namespace.
449 if (!NS.Verify())
450 return;
451
452 unsigned Line = NS.getLineNumber();
453 if (Line == 0)
454 return;
455 StringRef FN = NS.getFilename();
456
Eric Christopherc2697f82013-10-19 01:04:47 +0000457 unsigned FileID =
458 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000459 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000460 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
461 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000462}
463
Eric Christopher92331fd2012-11-21 00:34:38 +0000464/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000465/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000466void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
467 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000468 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000469 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000470 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000471 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
472 else
David Blaikieea2605d2013-06-20 00:25:24 +0000473 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000474 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000475}
476
Devang Patelba5fbf12011-04-26 19:06:18 +0000477/// addRegisterOp - Add register operand.
Eric Christophera5a79422013-12-09 23:32:48 +0000478void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000479 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantla83cc8a2014-02-11 21:22:59 +0000480 int DWReg = RI->getDwarfRegNum(Reg, false);
481 bool isSubRegister = DWReg < 0;
482
483 unsigned Idx = 0;
484
485 // Go up the super-register chain until we hit a valid dwarf register number.
486 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
487 DWReg = RI->getDwarfRegNum(*SR, false);
488 if (DWReg >= 0)
489 Idx = RI->getSubRegIndex(*SR, Reg);
490 }
491
492 if (DWReg < 0) {
493 DEBUG(llvm::dbgs() << "Invalid Dwarf register number.\n");
494 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
495 return;
496 }
497
498 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000499 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000500 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000501 else {
David Blaikief2443192013-10-21 17:28:37 +0000502 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
503 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000504 }
Adrian Prantla83cc8a2014-02-11 21:22:59 +0000505
506 // Emit Mask
507 if (isSubRegister) {
508 unsigned Size = RI->getSubRegIdxSize(Idx);
509 unsigned Offset = RI->getSubRegIdxOffset(Idx);
510 if (Offset > 0) {
511 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
512 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
513 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
514 } else {
515 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
516 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
517 }
518 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000519}
520
521/// addRegisterOffset - Add register offset.
Eric Christophera5a79422013-12-09 23:32:48 +0000522void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
523 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000524 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
525 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
526 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
527 if (Reg == TRI->getFrameRegister(*Asm->MF))
528 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000529 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000530 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000531 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000532 else {
David Blaikief2443192013-10-21 17:28:37 +0000533 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
534 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000535 }
David Blaikief2443192013-10-21 17:28:37 +0000536 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000537}
538
539/// addAddress - Add an address attribute to a die based on the location
540/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000541void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
542 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000543 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
544
David Blaikieea2605d2013-06-20 00:25:24 +0000545 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000546 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000547 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000548 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000549 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000550 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000551 }
552 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000553
554 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000555 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000556}
557
Devang Patel0e821f42011-04-12 23:21:44 +0000558/// addComplexAddress - Start with the address based on the location provided,
559/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000560/// given the extra address information encoded in the DbgVariable, starting
561/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000562///
Eric Christophera5a79422013-12-09 23:32:48 +0000563void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
564 dwarf::Attribute Attribute,
565 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000566 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000567 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000568 unsigned i = 0;
569 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000570 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000571 // If first address element is OpPlus then emit
572 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000573 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000574 i = 2;
575 } else
576 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000577 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000578 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000579
Eric Christopherc2697f82013-10-19 01:04:47 +0000580 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000581 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000582 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000583 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
584 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000585 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000586 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000587 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000588 } else
589 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000590 }
591
592 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000593 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000594}
595
596/* Byref variables, in Blocks, are declared by the programmer as "SomeType
597 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
598 gives the variable VarName either the struct, or a pointer to the struct, as
599 its type. This is necessary for various behind-the-scenes things the
600 compiler needs to do with by-reference variables in Blocks.
601
602 However, as far as the original *programmer* is concerned, the variable
603 should still have type 'SomeType', as originally declared.
604
605 The function getBlockByrefType dives into the __Block_byref_x_VarName
606 struct to find the original type of the variable, which is then assigned to
607 the variable's Debug Information Entry as its real type. So far, so good.
608 However now the debugger will expect the variable VarName to have the type
609 SomeType. So we need the location attribute for the variable to be an
610 expression that explains to the debugger how to navigate through the
611 pointers and struct to find the actual variable of type SomeType.
612
613 The following function does just that. We start by getting
614 the "normal" location for the variable. This will be the location
615 of either the struct __Block_byref_x_VarName or the pointer to the
616 struct __Block_byref_x_VarName.
617
618 The struct will look something like:
619
620 struct __Block_byref_x_VarName {
621 ... <various fields>
622 struct __Block_byref_x_VarName *forwarding;
623 ... <various other fields>
624 SomeType VarName;
625 ... <maybe more fields>
626 };
627
628 If we are given the struct directly (as our starting point) we
629 need to tell the debugger to:
630
631 1). Add the offset of the forwarding field.
632
633 2). Follow that pointer to get the real __Block_byref_x_VarName
634 struct to use (the real one may have been copied onto the heap).
635
636 3). Add the offset for the field VarName, to find the actual variable.
637
638 If we started with a pointer to the struct, then we need to
639 dereference that pointer first, before the other steps.
640 Translating this into DWARF ops, we will need to append the following
641 to the current location description for the variable:
642
643 DW_OP_deref -- optional, if we start with a pointer
644 DW_OP_plus_uconst <forward_fld_offset>
645 DW_OP_deref
646 DW_OP_plus_uconst <varName_fld_offset>
647
648 That is what this function does. */
649
650/// addBlockByrefAddress - Start with the address based on the location
651/// provided, and generate the DWARF information necessary to find the
652/// actual Block variable (navigating the Block struct) based on the
653/// starting location. Add the DWARF information to the die. For
654/// more information, read large comment just above here.
655///
Eric Christophera5a79422013-12-09 23:32:48 +0000656void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
657 dwarf::Attribute Attribute,
658 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000659 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000660 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000661 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000662 bool isPointer = false;
663
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000664 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000665
666 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000667 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000668 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000669 isPointer = true;
670 }
671
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000672 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000673
674 // Find the __forwarding field and the variable field in the __Block_byref
675 // struct.
676 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000677 DIDerivedType varField;
678 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000679
680 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000681 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000682 StringRef fieldName = DT.getName();
683 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000684 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000685 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000686 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000687 }
688
689 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000690 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
691 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000692
693 // Decode the original location, and use that as the start of the byref
694 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000695 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
696
Eric Christopheref9d7102012-07-04 02:02:18 +0000697 if (Location.isReg())
698 addRegisterOp(Block, Location.getReg());
699 else
700 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000701
702 // If we started with a pointer to the __Block_byref... struct, then
703 // the first thing we need to do is dereference the pointer (DW_OP_deref).
704 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000705 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000706
707 // Next add the offset for the '__forwarding' field:
708 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
709 // adding the offset if it's 0.
710 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000711 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
712 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000713 }
714
715 // Now dereference the __forwarding field to get to the real __Block_byref
716 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000717 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000718
719 // Now that we've got the real __Block_byref... struct, add the offset
720 // for the variable's field to get to the location of the actual variable:
721 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
722 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000723 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
724 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000725 }
726
727 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000728 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000729}
730
Devang Patelbcd50a12011-07-20 21:57:04 +0000731/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000732static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000733 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000734 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
735 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000736 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000737 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
738 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000739 *SizeInBits = Ty.getSizeInBits();
740 return true;
741 }
742 return false;
743}
744
Manman Renb3388602013-10-05 01:43:03 +0000745/// Return true if type encoding is unsigned.
746static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
747 DIDerivedType DTy(Ty);
748 if (DTy.isDerivedType())
749 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
750
751 DIBasicType BTy(Ty);
752 if (BTy.isBasicType()) {
753 unsigned Encoding = BTy.getEncoding();
754 if (Encoding == dwarf::DW_ATE_unsigned ||
755 Encoding == dwarf::DW_ATE_unsigned_char ||
756 Encoding == dwarf::DW_ATE_boolean)
757 return true;
758 }
759 return false;
760}
761
762/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000763static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000764 unsigned Tag = Ty.getTag();
765
766 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
767 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
768 Tag != dwarf::DW_TAG_restrict_type)
769 return Ty.getSizeInBits();
770
771 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
772
773 // If this type is not derived from any type then take conservative approach.
774 if (!BaseType.isValid())
775 return Ty.getSizeInBits();
776
777 // If this is a derived type, go ahead and get the base type, unless it's a
778 // reference then it's just the size of the field. Pointer types have no need
779 // of this since they're a different type of qualification on the type.
780 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
781 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
782 return Ty.getSizeInBits();
783
784 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000785 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000786
787 return BaseType.getSizeInBits();
788}
789
Devang Patel0e821f42011-04-12 23:21:44 +0000790/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000791void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
792 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000793 // FIXME: This is a bit conservative/simple - it emits negative values at
794 // their maximum bit width which is a bit unfortunate (& doesn't prefer
795 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000796 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000797 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000798 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000799 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000800
Eric Christopher9d1daa82013-08-27 23:49:04 +0000801 // If we're a signed constant definitely use sdata.
802 if (SignedConstant) {
803 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
804 return;
805 }
806
807 // Else use data for now unless it's larger than we can deal with.
808 switch (SizeInBits) {
809 case 8:
810 Form = dwarf::DW_FORM_data1;
811 break;
812 case 16:
813 Form = dwarf::DW_FORM_data2;
814 break;
815 case 32:
816 Form = dwarf::DW_FORM_data4;
817 break;
818 case 64:
819 Form = dwarf::DW_FORM_data8;
820 break;
821 default:
822 Form = dwarf::DW_FORM_udata;
823 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
824 return;
825 }
826 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000827}
828
829/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000830void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000831 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000832 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
833 APFloat FPImm = MO.getFPImm()->getValueAPF();
834
835 // Get the raw data form of the floating point.
836 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000837 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000838
839 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000840 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000841 int Incr = (LittleEndian ? 1 : -1);
842 int Start = (LittleEndian ? 0 : NumBytes - 1);
843 int Stop = (LittleEndian ? NumBytes : -1);
844
845 // Output the constant to DWARF one byte at a time.
846 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000847 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000848
David Blaikief2443192013-10-21 17:28:37 +0000849 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000850}
851
David Blaikiea39a76e2013-01-20 01:18:01 +0000852/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000853void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000854 // Pass this down to addConstantValue as an unsigned bag of bits.
855 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000856}
857
Devang Patel0e821f42011-04-12 23:21:44 +0000858/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000859void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
860 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000861 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000862}
863
864// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000865void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000866 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000867 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000868 // If we're a signed constant definitely use sdata.
869 if (!Unsigned) {
870 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
871 Val.getSExtValue());
872 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000873 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000874
875 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000876 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000877 switch (CIBitWidth) {
878 case 8:
879 Form = dwarf::DW_FORM_data1;
880 break;
881 case 16:
882 Form = dwarf::DW_FORM_data2;
883 break;
884 case 32:
885 Form = dwarf::DW_FORM_data4;
886 break;
887 case 64:
888 Form = dwarf::DW_FORM_data8;
889 break;
890 default:
891 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
892 Val.getZExtValue());
893 return;
894 }
895 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000896 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000897 }
898
899 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
900
901 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000902 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000903
904 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000905 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000906
907 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000908 for (int i = 0; i < NumBytes; i++) {
909 uint8_t c;
910 if (LittleEndian)
911 c = Ptr64[i / 8] >> (8 * (i & 7));
912 else
913 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000914 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000915 }
Devang Patel0e821f42011-04-12 23:21:44 +0000916
David Blaikief2443192013-10-21 17:28:37 +0000917 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000918}
919
Eric Christopher25e35092013-04-22 07:47:40 +0000920/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000921void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000922 // Add template parameters.
923 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
924 DIDescriptor Element = TParams.getElement(i);
925 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000926 constructTemplateTypeParameterDIE(Buffer,
927 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000928 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000929 constructTemplateValueParameterDIE(Buffer,
930 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000931 }
Devang Patel0e821f42011-04-12 23:21:44 +0000932}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000933
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000934/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000935DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000936 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000937 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000938 if (Context.isType())
939 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000940 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000941 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000942 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000943 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000944 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000945}
946
Eric Christophera5a79422013-12-09 23:32:48 +0000947DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000948 DIScope Context = resolve(Ty.getContext());
949 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000950
951 DIE *TyDIE = getDIE(Ty);
952 if (TyDIE)
953 return TyDIE;
954
955 // Create new type.
956 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
957
David Blaikiefbd29eb2013-11-26 00:35:04 +0000958 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000959
David Blaikie9d861be2013-11-26 00:15:27 +0000960 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000961 return TyDIE;
962}
963
Devang Patel0e821f42011-04-12 23:21:44 +0000964/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
965/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000966DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000967 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000968 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000969
David Blaikie32887552013-11-14 22:25:02 +0000970 DIType Ty(TyNode);
971 assert(Ty.isType());
972
Manman Renf4c339e2013-10-29 22:49:29 +0000973 // Construct the context before querying for the existence of the DIE in case
974 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000975 DIScope Context = resolve(Ty.getContext());
976 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000977 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000978
Eric Christophere595bae2013-10-04 17:08:38 +0000979 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000980 if (TyDIE)
981 return TyDIE;
982
983 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000984 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
985
Devang Patel0e821f42011-04-12 23:21:44 +0000986 if (Ty.isBasicType())
987 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000988 else if (Ty.isCompositeType()) {
989 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000990 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
991 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikief645f962014-01-09 03:23:41 +0000992 DD->addDwarfTypeUnitType(getCUNode(), TypeId->getString(), TyDIE, CTy);
David Blaikiecfb21152014-01-03 18:59:42 +0000993 // Skip updating the accellerator tables since this is not the full type
994 return TyDIE;
995 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000996 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000997 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000998 assert(Ty.isDerivedType() && "Unknown kind of DIType");
999 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1000 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001001
David Blaikie9d861be2013-11-26 00:15:27 +00001002 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +00001003
1004 return TyDIE;
1005}
1006
Eric Christophera5a79422013-12-09 23:32:48 +00001007void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1008 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001009 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1010 bool IsImplementation = 0;
1011 if (Ty.isCompositeType()) {
1012 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001013 // A runtime language of 0 actually means C/C++ and that any
1014 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001015 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001016 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001017 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001018 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001019
1020 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1021 Context.isNameSpace())
1022 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001023 }
Devang Patel0e821f42011-04-12 23:21:44 +00001024}
1025
1026/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001027void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001028 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001029
1030 // Check for pre-existence.
1031 DIEEntry *Entry = getDIEEntry(Ty);
1032 // If it exists then use the existing value.
1033 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001034 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001035 return;
1036 }
1037
1038 // Construct type.
1039 DIE *Buffer = getOrCreateTypeDIE(Ty);
1040
1041 // Set up proxy.
1042 Entry = createDIEEntry(Buffer);
1043 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001044 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001045}
1046
Eric Christopher9cd26af2013-09-20 23:22:52 +00001047// Accelerator table mutators - add each name along with its companion
1048// DIE to the proper table while ensuring that the name that we're going
1049// to reference is in the string table. We do this since the names we
1050// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001051void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001052 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001053 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001054 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001055 DIEs.push_back(Die);
1056}
1057
Eric Christophera5a79422013-12-09 23:32:48 +00001058void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001059 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001060 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001061 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001062 DIEs.push_back(Die);
1063}
1064
Eric Christophera5a79422013-12-09 23:32:48 +00001065void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001066 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001067 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001068 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001069 DIEs.push_back(Die);
1070}
1071
Eric Christophera5a79422013-12-09 23:32:48 +00001072void DwarfUnit::addAccelType(StringRef Name,
1073 std::pair<const DIE *, unsigned> Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001074 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001075 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001076 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001077 DIEs.push_back(Die);
1078}
1079
Eric Christopher9c58f312013-09-20 22:20:55 +00001080/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001081void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001082 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001083 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001084}
1085
Eric Christopher2c8b7902013-10-17 02:06:06 +00001086/// getParentContextString - Walks the metadata parent chain in a language
1087/// specific manner (using the compile unit language) and returns
1088/// it as a string. This is done at the metadata level because DIEs may
1089/// not currently have been added to the parent context and walking the
1090/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001091std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001092 if (!Context)
1093 return "";
1094
1095 // FIXME: Decide whether to implement this for non-C++ languages.
1096 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1097 return "";
1098
Eric Christopher8dba0d52013-10-19 01:04:42 +00001099 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001100 SmallVector<DIScope, 1> Parents;
1101 while (!Context.isCompileUnit()) {
1102 Parents.push_back(Context);
1103 if (Context.getContext())
1104 Context = resolve(Context.getContext());
1105 else
1106 // Structure, etc types will have a NULL context if they're at the top
1107 // level.
1108 break;
1109 }
1110
1111 // Reverse iterate over our list to go from the outermost construct to the
1112 // innermost.
1113 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1114 E = Parents.rend();
1115 I != E; ++I) {
1116 DIScope Ctx = *I;
1117 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001118 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001119 CS += Name;
1120 CS += "::";
1121 }
1122 }
1123 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001124}
1125
1126/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001127void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001128 // Get core information.
1129 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001130 // Add name if not anonymous or intermediate type.
1131 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001132 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001133
David Blaikiefac56122013-10-04 23:21:16 +00001134 // An unspecified type only has a name attribute.
1135 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001136 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001137
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001138 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001139 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001140
Devang Patel0e821f42011-04-12 23:21:44 +00001141 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001142 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001143}
1144
1145/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001146void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001147 // Get core information.
1148 StringRef Name = DTy.getName();
1149 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001150 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001151
1152 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001153 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001154 if (FromTy)
1155 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001156
1157 // Add name if not anonymous or intermediate type.
1158 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001159 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001160
1161 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001162 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001163 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001164
David Blaikie5d3249b2013-01-07 05:51:15 +00001165 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001166 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1167 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001168 // Add source line info if available and TyDesc is not a forward declaration.
1169 if (!DTy.isForwardDecl())
1170 addSourceLine(&Buffer, DTy);
1171}
1172
1173/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001174void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001175 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001176 StringRef Name = CTy.getName();
1177
1178 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001179 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001180
1181 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001182 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001183 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001184 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001185 case dwarf::DW_TAG_enumeration_type:
1186 constructEnumTypeDIE(Buffer, CTy);
1187 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001188 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001189 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001190 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001191 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001192 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001193 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001194
1195 bool isPrototyped = true;
1196 // Add arguments.
1197 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1198 DIDescriptor Ty = Elements.getElement(i);
1199 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001200 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001201 isPrototyped = false;
1202 } else {
Manman Renb987e512013-10-29 00:53:03 +00001203 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001204 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001205 if (DIType(Ty).isArtificial())
1206 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001207 }
1208 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001209 // Add prototype flag if we're dealing with a C language and the
1210 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001211 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001212 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001213 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001214 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001215 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001216
1217 if (CTy.isLValueReference())
1218 addFlag(&Buffer, dwarf::DW_AT_reference);
1219
1220 if (CTy.isRValueReference())
1221 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001222 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001223 case dwarf::DW_TAG_structure_type:
1224 case dwarf::DW_TAG_union_type:
1225 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001226 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001227 DIArray Elements = CTy.getTypeArray();
1228 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001229 DIDescriptor Element = Elements.getElement(i);
1230 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001231 if (Element.isSubprogram())
1232 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1233 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001234 DIDerivedType DDTy(Element);
1235 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001236 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001237 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001238 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001239 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001240 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001241 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001242 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001243 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001244 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001245 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001246 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001247 StringRef PropertyName = Property.getObjCPropertyName();
1248 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001249 if (Property.getType())
1250 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001251 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001252 StringRef GetterName = Property.getObjCPropertyGetterName();
1253 if (!GetterName.empty())
1254 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1255 StringRef SetterName = Property.getObjCPropertySetterName();
1256 if (!SetterName.empty())
1257 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1258 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001259 if (Property.isReadOnlyObjCProperty())
1260 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1261 if (Property.isReadWriteObjCProperty())
1262 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1263 if (Property.isAssignObjCProperty())
1264 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1265 if (Property.isRetainObjCProperty())
1266 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1267 if (Property.isCopyObjCProperty())
1268 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1269 if (Property.isNonAtomicObjCProperty())
1270 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1271 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001272 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001273 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001274
Devang Pateld925d1a2012-02-07 23:33:58 +00001275 DIEEntry *Entry = getDIEEntry(Element);
1276 if (!Entry) {
1277 Entry = createDIEEntry(ElemDie);
1278 insertDIEEntry(Element, Entry);
1279 }
Devang Patel403e8192012-02-04 01:30:32 +00001280 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001281 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001282 }
1283
1284 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001285 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001286
Eric Christopher9e429ae2013-10-05 00:27:02 +00001287 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001288 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001289 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001290 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001291
Devang Patel12419ae2011-05-12 21:29:42 +00001292 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001293 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001294
Eric Christopherda011dd2011-12-16 23:42:42 +00001295 // Add template parameters to a class, structure or union types.
1296 // FIXME: The support isn't in the metadata for this yet.
1297 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001298 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001299 addTemplateParams(Buffer, CTy.getTemplateParams());
1300
1301 break;
1302 }
1303 default:
1304 break;
1305 }
1306
1307 // Add name if not anonymous or intermediate type.
1308 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001309 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001310
Eric Christopher775cbd22012-05-22 18:45:18 +00001311 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001312 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001313 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001314 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001315 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001316 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001317 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001318 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001319 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001320 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001321
1322 // If we're a forward decl, say so.
1323 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001324 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001325
1326 // Add source line info if available.
1327 if (!CTy.isForwardDecl())
1328 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001329
1330 // No harm in adding the runtime language to the declaration.
1331 unsigned RLang = CTy.getRunTimeLang();
1332 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001333 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1334 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001335 }
1336}
1337
Manman Renffc9a712013-10-23 23:05:28 +00001338/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1339/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001340void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1341 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001342 DIE *ParamDIE =
1343 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001344 // Add the type if it exists, it could be void and therefore no type.
1345 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001346 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001347 if (!TP.getName().empty())
1348 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001349}
1350
Manman Renffc9a712013-10-23 23:05:28 +00001351/// constructTemplateValueParameterDIE - Construct new DIE for the given
1352/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001353void
1354DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001355 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001356 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001357
1358 // Add the type if there is one, template template and template parameter
1359 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001360 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001361 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001362 if (!VP.getName().empty())
1363 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1364 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001365 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001366 addConstantValue(ParamDIE, CI,
1367 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001368 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1369 // For declaration non-type template parameters (such as global values and
1370 // functions)
1371 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001372 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001373 // Emit DW_OP_stack_value to use the address as the immediate value of the
1374 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001375 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1376 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001377 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001378 assert(isa<MDString>(Val));
1379 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1380 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001381 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001382 assert(isa<MDNode>(Val));
1383 DIArray A(cast<MDNode>(Val));
1384 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001385 }
1386 }
Devang Patel0e821f42011-04-12 23:21:44 +00001387}
1388
Devang Patel17b53272011-05-06 16:57:54 +00001389/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001390DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001391 // Construct the context before querying for the existence of the DIE in case
1392 // such construction creates the DIE.
1393 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001394
Devang Patel17b53272011-05-06 16:57:54 +00001395 DIE *NDie = getDIE(NS);
1396 if (NDie)
1397 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001398 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1399
Eric Christopher4996c702011-11-07 09:24:32 +00001400 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001401 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001402 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001403 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001404 } else
1405 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001406 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001407 return NDie;
1408}
1409
Devang Patel89543712011-08-15 17:24:54 +00001410/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001411DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001412 // Construct the context before querying for the existence of the DIE in case
1413 // such construction creates the DIE (as is the case for member function
1414 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001415 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001416
Eric Christophere595bae2013-10-04 17:08:38 +00001417 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001418 if (SPDie)
1419 return SPDie;
1420
Manman Ren73d697c2013-10-29 00:58:04 +00001421 DISubprogram SPDecl = SP.getFunctionDeclaration();
1422 if (SPDecl.isSubprogram())
1423 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001424 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001425
1426 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001427 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001428
Rafael Espindola79278362011-11-10 22:34:29 +00001429 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001430 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001431 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001432
Devang Patel89543712011-08-15 17:24:54 +00001433 // Add function template parameters.
1434 addTemplateParams(*SPDie, SP.getTemplateParams());
1435
Devang Patel89543712011-08-15 17:24:54 +00001436 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001437 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001438 if (DeclDie) {
1439 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001440 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001441
Devang Patel89543712011-08-15 17:24:54 +00001442 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001443 }
Devang Patel89543712011-08-15 17:24:54 +00001444
Eric Christopheracb71152012-08-23 22:52:55 +00001445 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001446 StringRef LinkageName = SP.getLinkageName();
1447 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001448 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001449 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001450
Devang Patel89543712011-08-15 17:24:54 +00001451 // Constructors and operators for anonymous aggregates do not have names.
1452 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001453 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001454
1455 addSourceLine(SPDie, SP);
1456
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001457 // Add the prototype if we have a prototype and we have a C like
1458 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001459 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001460 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001461 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001462 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001463 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001464
Devang Patel89543712011-08-15 17:24:54 +00001465 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001466 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1467 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001468
David Blaikie5174c842013-05-22 23:22:18 +00001469 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001470 // Add a return type. If this is a type like a C/C++ void type we don't add a
1471 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001472 if (Args.getElement(0))
1473 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001474
1475 unsigned VK = SP.getVirtuality();
1476 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001477 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001478 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001479 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1480 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1481 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001482 ContainingTypeMap.insert(
1483 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001484 }
1485
1486 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001487 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001488
Devang Patel89543712011-08-15 17:24:54 +00001489 // Add arguments. Do not add arguments for subprogram definition. They will
1490 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001491 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001492 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001493 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001494 addType(Arg, ATy);
1495 if (ATy.isArtificial())
1496 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001497 }
Devang Patel89543712011-08-15 17:24:54 +00001498 }
1499
1500 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001501 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001502
1503 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001504 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001505
1506 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001507 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001508
1509 if (unsigned isa = Asm->getISAEncoding()) {
1510 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1511 }
1512
Adrian Prantl99c7af22013-12-18 21:48:19 +00001513 if (SP.isLValueReference())
1514 addFlag(SPDie, dwarf::DW_AT_reference);
1515
1516 if (SP.isRValueReference())
1517 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1518
Adrian Prantlef129fb2014-01-18 02:12:00 +00001519 if (SP.isProtected())
1520 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1521 dwarf::DW_ACCESS_protected);
1522 else if (SP.isPrivate())
1523 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1524 dwarf::DW_ACCESS_private);
1525 else
1526 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1527 dwarf::DW_ACCESS_public);
1528
1529 if (SP.isExplicit())
1530 addFlag(SPDie, dwarf::DW_AT_explicit);
1531
Devang Patel89543712011-08-15 17:24:54 +00001532 return SPDie;
1533}
1534
Devang Pateldfd6ec32011-08-15 17:57:41 +00001535// Return const expression if value is a GEP to access merged global
1536// constant. e.g.
1537// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1538static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1539 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1540 if (!CE || CE->getNumOperands() != 3 ||
1541 CE->getOpcode() != Instruction::GetElementPtr)
1542 return NULL;
1543
1544 // First operand points to a global struct.
1545 Value *Ptr = CE->getOperand(0);
1546 if (!isa<GlobalValue>(Ptr) ||
1547 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1548 return NULL;
1549
1550 // Second operand is zero.
1551 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1552 if (!CI || !CI->isZero())
1553 return NULL;
1554
1555 // Third operand is offset.
1556 if (!isa<ConstantInt>(CE->getOperand(2)))
1557 return NULL;
1558
1559 return CE;
1560}
1561
1562/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001563void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001564 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001565 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001566 return;
1567
David Blaikie5e390e42014-02-04 01:23:52 +00001568 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001569
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001570 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001571 DIType GTy = GV.getType();
1572
1573 // If this is a static data member definition, some attributes belong
1574 // to the declaration DIE.
1575 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001576 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001577 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1578 if (SDMDecl.Verify()) {
1579 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1580 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001581 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001582 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001583 }
1584
1585 // If this is not a static data member definition, create the variable
1586 // DIE and add the initial set of attributes to it.
1587 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001588 // Construct the context before querying for the existence of the DIE in
1589 // case such construction creates the DIE.
1590 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001591
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001592 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001593 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001594
1595 // Add name and type.
1596 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1597 addType(VariableDIE, GTy);
1598
1599 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001600 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001601 addFlag(VariableDIE, dwarf::DW_AT_external);
1602
1603 // Add line number info.
1604 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001605 }
1606
Devang Pateldfd6ec32011-08-15 17:57:41 +00001607 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001608 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001609 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001610 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001611 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001612 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001613 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001614 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001615 if (GV.getGlobal()->isThreadLocal()) {
1616 // FIXME: Make this work with -gsplit-dwarf.
1617 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1618 assert((PointerSize == 4 || PointerSize == 8) &&
1619 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001620 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001621 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001622 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001623 if (!DD->useSplitDwarf()) {
1624 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001625 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001626 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001627 // 2) containing the (relocated) offset of the TLS variable
1628 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001629 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001630 } else {
David Blaikief2443192013-10-21 17:28:37 +00001631 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1632 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001633 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001634 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001635 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001636 } else {
1637 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001638 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001639 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001640 // Do not create specification DIE if context is either compile unit
1641 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001642 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001643 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001644 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001645 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001646 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001647 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001648 // A static member's declaration is already flagged as such.
1649 if (!SDMDecl.Verify())
1650 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001651 } else {
David Blaikief2443192013-10-21 17:28:37 +00001652 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001653 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001654 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001655 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001656 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001657 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1658 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1659 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001660 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1661 : VariableDIE,
1662 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001663 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001664 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001665 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001666 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001667 // emitting AT_const_value multiple times, we only add AT_const_value when
1668 // it is not a static member.
1669 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001670 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001671 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001672 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001673 // GV is a merged global.
1674 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1675 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001676 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1677 DD->addArangeLabel(SymbolCU(this, Sym));
1678 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001679 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001680 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001681 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001682 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001683 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1684 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001685 }
1686
Eric Christopherc12c2112011-11-11 01:55:22 +00001687 if (addToAccelTable) {
1688 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1689 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001690
Eric Christopherc12c2112011-11-11 01:55:22 +00001691 // If the linkage name is different than the name, go ahead and output
1692 // that as well into the name table.
1693 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1694 addAccelName(GV.getLinkageName(), AddrDIE);
1695 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001696
1697 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001698 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1699 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001700}
1701
Devang Patel0e821f42011-04-12 23:21:44 +00001702/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001703void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001704 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001705 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001706
Bill Wendling28fe9e72012-12-06 07:38:10 +00001707 // The LowerBound value defines the lower bounds which is typically zero for
1708 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1709 // Count == -1 then the array is unbounded and we do not emit
1710 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1711 // Count == 0, then the array has zero elements in which case we do not emit
1712 // an upper bound.
1713 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001714 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001715 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001716
Bill Wendling3495f9b2012-12-06 07:55:19 +00001717 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001718 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001719
1720 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001721 // FIXME: An unbounded array should reference the expression that defines
1722 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001723 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1724 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001725}
1726
1727/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001728void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001729 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001730 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001731
Eric Christopher0df08e22013-08-08 07:40:37 +00001732 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001733 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001734
1735 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001736 // FIXME: This type should be passed down from the front end
1737 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001738 DIE *IdxTy = getIndexTyDie();
1739 if (!IdxTy) {
1740 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001741 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001742 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001743 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001744 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1745 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001746 setIndexTyDie(IdxTy);
1747 }
1748
1749 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001750 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001751 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1752 DIDescriptor Element = Elements.getElement(i);
1753 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1754 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1755 }
1756}
1757
Eric Christopheraeb105f2013-11-11 18:52:39 +00001758/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001759void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001760 DIArray Elements = CTy.getTypeArray();
1761
1762 // Add enumerators to enumeration type.
1763 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001764 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001765 if (Enum.isEnumerator()) {
1766 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001767 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001768 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001769 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001770 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1771 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001772 }
1773 }
1774 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1775 if (DTy) {
1776 addType(&Buffer, DTy);
1777 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1778 }
Devang Patel0e821f42011-04-12 23:21:44 +00001779}
1780
Devang Patel89543712011-08-15 17:24:54 +00001781/// constructContainingTypeDIEs - Construct DIEs for types that contain
1782/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001783void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001784 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001785 CE = ContainingTypeMap.end();
1786 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001787 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001788 DIDescriptor D(CI->second);
1789 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001790 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001791 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001792 if (!NDie)
1793 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001794 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001795 }
1796}
1797
Devang Patel3acc70e2011-08-15 22:04:40 +00001798/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001799DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001800 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001801
Devang Patel3acc70e2011-08-15 22:04:40 +00001802 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001803 DIE *VariableDie = new DIE(DV.getTag());
1804 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001805 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001806 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001807 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001808 else {
David Blaikie715528b2013-08-19 03:34:03 +00001809 if (!Name.empty())
1810 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001811 addSourceLine(VariableDie, DV.getVariable());
1812 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001813 }
1814
Eric Christopher34a2c872013-11-15 01:43:19 +00001815 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001816 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001817
1818 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001819 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001820 return VariableDie;
1821 }
1822
1823 // Add variable address.
1824
Eric Christopher34a2c872013-11-15 01:43:19 +00001825 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001826 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001827 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1828 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001829 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001830 return VariableDie;
1831 }
1832
Eric Christophercead0332011-10-03 15:49:20 +00001833 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001834 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001835 assert(DVInsn->getNumOperands() == 3);
1836 if (DVInsn->getOperand(0).isReg()) {
1837 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001838 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001839 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001840 MachineLocation Location(RegOp.getReg(),
1841 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001842 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001843 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001844 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001845 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001846 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001847 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001848 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001849 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001850 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001851 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001852
Eric Christopher34a2c872013-11-15 01:43:19 +00001853 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001854 return VariableDie;
1855 } else {
1856 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001857 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001858 if (FI != ~0) {
1859 unsigned FrameReg = 0;
1860 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001861 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001862 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001863 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001864 }
1865 }
1866
Eric Christopher34a2c872013-11-15 01:43:19 +00001867 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001868 return VariableDie;
1869}
1870
Manman Ren230ec862013-10-23 23:00:44 +00001871/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001872void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001873 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001874 StringRef Name = DT.getName();
1875 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001876 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001877
Manman Ren93b30902013-10-08 18:42:58 +00001878 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001879
1880 addSourceLine(MemberDie, DT);
1881
Eric Christopherc2697f82013-10-19 01:04:47 +00001882 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001883
1884 // For C++, virtual base classes are not at fixed offset. Use following
1885 // expression to extract appropriate offset from vtable.
1886 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1887
1888 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001889 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1890 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1891 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1892 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1893 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1894 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1895 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001896
David Blaikief2443192013-10-21 17:28:37 +00001897 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001898 } else {
1899 uint64_t Size = DT.getSizeInBits();
1900 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1901 uint64_t OffsetInBytes;
1902
1903 if (Size != FieldSize) {
1904 // Handle bitfield.
1905 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1906 getBaseTypeSize(DD, DT) >> 3);
1907 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1908
1909 uint64_t Offset = DT.getOffsetInBits();
1910 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1911 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1912 uint64_t FieldOffset = (HiMark - FieldSize);
1913 Offset -= FieldOffset;
1914
1915 // Maybe we need to work from the other end.
1916 if (Asm->getDataLayout().isLittleEndian())
1917 Offset = FieldSize - (Offset + Size);
1918 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1919
Adrian Prantlc67655a2014-01-28 18:13:47 +00001920 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001921 // field that includes this bit field.
1922 OffsetInBytes = FieldOffset >> 3;
1923 } else
1924 // This is not a bitfield.
1925 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001926
David Blaikie22b29a52014-01-03 01:30:05 +00001927 if (DD->getDwarfVersion() <= 2) {
1928 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1929 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1930 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1931 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1932 } else
1933 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1934 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001935 }
Devang Patel0e821f42011-04-12 23:21:44 +00001936
1937 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001938 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001939 dwarf::DW_ACCESS_protected);
1940 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001941 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001942 dwarf::DW_ACCESS_private);
1943 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001944 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001945 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001946 dwarf::DW_ACCESS_public);
1947 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001948 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001949 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001950
1951 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001952 if (MDNode *PNode = DT.getObjCProperty())
1953 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001954 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001955 PropertyDie);
1956
David Blaikie37fefc32012-12-13 22:43:07 +00001957 if (DT.isArtificial())
1958 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001959}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001960
Manman Renc6b63922013-10-14 20:33:57 +00001961/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001962DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001963 if (!DT.Verify())
1964 return NULL;
1965
Manman Renc6b63922013-10-14 20:33:57 +00001966 // Construct the context before querying for the existence of the DIE in case
1967 // such construction creates the DIE.
1968 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001969 assert(dwarf::isType(ContextDIE->getTag()) &&
1970 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001971
1972 DIE *StaticMemberDIE = getDIE(DT);
1973 if (StaticMemberDIE)
1974 return StaticMemberDIE;
1975
Manman Renb987e512013-10-29 00:53:03 +00001976 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001977
Manman Ren93b30902013-10-08 18:42:58 +00001978 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001979
1980 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1981 addType(StaticMemberDIE, Ty);
1982 addSourceLine(StaticMemberDIE, DT);
1983 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1984 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1985
1986 // FIXME: We could omit private if the parent is a class_type, and
1987 // public if the parent is something else.
1988 if (DT.isProtected())
1989 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1990 dwarf::DW_ACCESS_protected);
1991 else if (DT.isPrivate())
1992 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1993 dwarf::DW_ACCESS_private);
1994 else
1995 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1996 dwarf::DW_ACCESS_public);
1997
1998 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001999 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00002000 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
2001 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002002
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002003 return StaticMemberDIE;
2004}
David Blaikie6b288cf2013-10-30 20:42:41 +00002005
Eric Christophera5a79422013-12-09 23:32:48 +00002006void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00002007 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00002008 Asm->OutStreamer.AddComment("DWARF version number");
2009 Asm->EmitInt16(DD->getDwarfVersion());
2010 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00002011 // We share one abbreviations table across all units so it's always at the
2012 // start of the section. Use a relocatable offset where needed to ensure
2013 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00002014 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00002015 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2016 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2017}
David Blaikiebc563272013-12-13 21:33:40 +00002018
Juergen Ributzkadb9ee002013-12-14 12:23:14 +00002019DwarfCompileUnit::~DwarfCompileUnit() {}
2020DwarfTypeUnit::~DwarfTypeUnit() {}
2021
David Blaikiebc563272013-12-13 21:33:40 +00002022void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2023 const MCSymbol *ASectionSym) const {
2024 DwarfUnit::emitHeader(ASection, ASectionSym);
2025 Asm->OutStreamer.AddComment("Type Signature");
2026 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2027 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002028 // In a skeleton type unit there is no type DIE so emit a zero offset.
2029 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2030 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002031}
2032
2033void DwarfTypeUnit::initSection(const MCSection *Section) {
2034 assert(!this->Section);
2035 this->Section = Section;
2036 // Since each type unit is contained in its own COMDAT section, the begin
2037 // label and the section label are the same. Using the begin label emission in
2038 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2039 // the only other alternative of lazily constructing start-of-section labels
2040 // and storing a mapping in DwarfDebug (or AsmPrinter).
2041 this->SectionSym = this->LabelBegin =
2042 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2043 this->LabelEnd =
2044 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2045 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2046}