blob: 89bb7fbf8152cfd440c3ffd61392046c5fc3f26d [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel0e821f42011-04-12 23:21:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher160522c2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel0e821f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
David Blaikie2c86a722013-12-02 19:33:15 +000016#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000026#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000027#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49}
50
Eric Christopher4287a492013-12-09 23:57:44 +000051DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000054 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000056}
57
David 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) {
David Blaikie52019302014-02-11 23:57:03 +0000359 assert(V.isVariable());
Eric Christopher92331fd2012-11-21 00:34:38 +0000360
Devang Patel0e821f42011-04-12 23:21:44 +0000361 unsigned Line = V.getLineNumber();
362 if (Line == 0)
363 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000364 unsigned FileID =
365 DD->getOrCreateSourceID(V.getContext().getFilename(),
366 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000367 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000368 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
369 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000370}
371
372/// addSourceLine - Add location information to specified debug information
373/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000374void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000375 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000376
377 unsigned Line = G.getLineNumber();
378 if (Line == 0)
379 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000380 unsigned FileID =
381 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000382 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000383 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
384 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000385}
386
387/// addSourceLine - Add location information to specified debug information
388/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000389void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000390 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000391
Devang Patel0e821f42011-04-12 23:21:44 +0000392 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000393 unsigned Line = SP.getLineNumber();
394 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000395 return;
396
Eric Christopherc2697f82013-10-19 01:04:47 +0000397 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
398 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000399 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000400 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
401 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000402}
403
404/// addSourceLine - Add location information to specified debug information
405/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000406void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000407 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000408
409 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000410 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000411 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000412 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
413 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000414 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000415 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
416 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000417}
418
419/// addSourceLine - Add location information to specified debug information
420/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000421void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000422 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000423
424 unsigned Line = Ty.getLineNumber();
425 if (Line == 0)
426 return;
427 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000428 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000429 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000430 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000431 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
432 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000433}
434
435/// addSourceLine - Add location information to specified debug information
436/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000437void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000438 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000439
440 unsigned Line = NS.getLineNumber();
441 if (Line == 0)
442 return;
443 StringRef FN = NS.getFilename();
444
Eric Christopherc2697f82013-10-19 01:04:47 +0000445 unsigned FileID =
446 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000447 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000448 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
449 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000450}
451
Eric Christopher92331fd2012-11-21 00:34:38 +0000452/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000453/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000454void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
455 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000456 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000457 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000458 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000459 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
460 else
David Blaikieea2605d2013-06-20 00:25:24 +0000461 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000462 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000463}
464
Devang Patelba5fbf12011-04-26 19:06:18 +0000465/// addRegisterOp - Add register operand.
Eric Christophera5a79422013-12-09 23:32:48 +0000466void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000467 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000468 int DWReg = RI->getDwarfRegNum(Reg, false);
469 bool isSubRegister = DWReg < 0;
470
471 unsigned Idx = 0;
472
473 // Go up the super-register chain until we hit a valid dwarf register number.
474 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
475 DWReg = RI->getDwarfRegNum(*SR, false);
476 if (DWReg >= 0)
477 Idx = RI->getSubRegIndex(*SR, Reg);
478 }
479
480 if (DWReg < 0) {
481 DEBUG(llvm::dbgs() << "Invalid Dwarf register number.\n");
482 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
483 return;
484 }
485
486 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000487 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000488 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000489 else {
David Blaikief2443192013-10-21 17:28:37 +0000490 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
491 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000492 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000493
494 // Emit Mask
495 if (isSubRegister) {
496 unsigned Size = RI->getSubRegIdxSize(Idx);
497 unsigned Offset = RI->getSubRegIdxOffset(Idx);
498 if (Offset > 0) {
499 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
500 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
501 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
502 } else {
503 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
504 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
505 }
506 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000507}
508
509/// addRegisterOffset - Add register offset.
Eric Christophera5a79422013-12-09 23:32:48 +0000510void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
511 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000512 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
513 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
514 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
515 if (Reg == TRI->getFrameRegister(*Asm->MF))
516 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000517 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000518 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000519 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000520 else {
David Blaikief2443192013-10-21 17:28:37 +0000521 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
522 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000523 }
David Blaikief2443192013-10-21 17:28:37 +0000524 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000525}
526
527/// addAddress - Add an address attribute to a die based on the location
528/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000529void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
530 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000531 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
532
David Blaikieea2605d2013-06-20 00:25:24 +0000533 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000534 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000535 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000536 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000537 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000538 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000539 }
540 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000541
542 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000543 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000544}
545
Devang Patel0e821f42011-04-12 23:21:44 +0000546/// addComplexAddress - Start with the address based on the location provided,
547/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000548/// given the extra address information encoded in the DbgVariable, starting
549/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000550///
Eric Christophera5a79422013-12-09 23:32:48 +0000551void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
552 dwarf::Attribute Attribute,
553 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000554 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000555 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000556 unsigned i = 0;
557 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000558 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000559 // If first address element is OpPlus then emit
560 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000561 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000562 i = 2;
563 } else
564 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000565 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000566 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000567
Eric Christopherc2697f82013-10-19 01:04:47 +0000568 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000569 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000570 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000571 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
572 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000573 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000574 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000575 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000576 } else
577 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000578 }
579
580 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000581 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000582}
583
584/* Byref variables, in Blocks, are declared by the programmer as "SomeType
585 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
586 gives the variable VarName either the struct, or a pointer to the struct, as
587 its type. This is necessary for various behind-the-scenes things the
588 compiler needs to do with by-reference variables in Blocks.
589
590 However, as far as the original *programmer* is concerned, the variable
591 should still have type 'SomeType', as originally declared.
592
593 The function getBlockByrefType dives into the __Block_byref_x_VarName
594 struct to find the original type of the variable, which is then assigned to
595 the variable's Debug Information Entry as its real type. So far, so good.
596 However now the debugger will expect the variable VarName to have the type
597 SomeType. So we need the location attribute for the variable to be an
598 expression that explains to the debugger how to navigate through the
599 pointers and struct to find the actual variable of type SomeType.
600
601 The following function does just that. We start by getting
602 the "normal" location for the variable. This will be the location
603 of either the struct __Block_byref_x_VarName or the pointer to the
604 struct __Block_byref_x_VarName.
605
606 The struct will look something like:
607
608 struct __Block_byref_x_VarName {
609 ... <various fields>
610 struct __Block_byref_x_VarName *forwarding;
611 ... <various other fields>
612 SomeType VarName;
613 ... <maybe more fields>
614 };
615
616 If we are given the struct directly (as our starting point) we
617 need to tell the debugger to:
618
619 1). Add the offset of the forwarding field.
620
621 2). Follow that pointer to get the real __Block_byref_x_VarName
622 struct to use (the real one may have been copied onto the heap).
623
624 3). Add the offset for the field VarName, to find the actual variable.
625
626 If we started with a pointer to the struct, then we need to
627 dereference that pointer first, before the other steps.
628 Translating this into DWARF ops, we will need to append the following
629 to the current location description for the variable:
630
631 DW_OP_deref -- optional, if we start with a pointer
632 DW_OP_plus_uconst <forward_fld_offset>
633 DW_OP_deref
634 DW_OP_plus_uconst <varName_fld_offset>
635
636 That is what this function does. */
637
638/// addBlockByrefAddress - Start with the address based on the location
639/// provided, and generate the DWARF information necessary to find the
640/// actual Block variable (navigating the Block struct) based on the
641/// starting location. Add the DWARF information to the die. For
642/// more information, read large comment just above here.
643///
Eric Christophera5a79422013-12-09 23:32:48 +0000644void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
645 dwarf::Attribute Attribute,
646 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000647 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000648 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000649 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000650 bool isPointer = false;
651
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000652 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000653
654 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000655 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000656 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000657 isPointer = true;
658 }
659
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000660 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000661
662 // Find the __forwarding field and the variable field in the __Block_byref
663 // struct.
664 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000665 DIDerivedType varField;
666 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000667
668 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000669 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000670 StringRef fieldName = DT.getName();
671 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000672 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000673 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000674 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000675 }
676
677 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000678 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
679 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000680
681 // Decode the original location, and use that as the start of the byref
682 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000683 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
684
Eric Christopheref9d7102012-07-04 02:02:18 +0000685 if (Location.isReg())
686 addRegisterOp(Block, Location.getReg());
687 else
688 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000689
690 // If we started with a pointer to the __Block_byref... struct, then
691 // the first thing we need to do is dereference the pointer (DW_OP_deref).
692 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000693 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000694
695 // Next add the offset for the '__forwarding' field:
696 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
697 // adding the offset if it's 0.
698 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000699 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
700 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000701 }
702
703 // Now dereference the __forwarding field to get to the real __Block_byref
704 // struct: DW_OP_deref.
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 // Now that we've got the real __Block_byref... struct, add the offset
708 // for the variable's field to get to the location of the actual variable:
709 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
710 if (varFieldOffset > 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, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000713 }
714
715 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000716 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000717}
718
Devang Patelbcd50a12011-07-20 21:57:04 +0000719/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000720static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000721 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000722 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
723 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000724 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000725 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
726 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000727 *SizeInBits = Ty.getSizeInBits();
728 return true;
729 }
730 return false;
731}
732
Manman Renb3388602013-10-05 01:43:03 +0000733/// Return true if type encoding is unsigned.
734static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
735 DIDerivedType DTy(Ty);
736 if (DTy.isDerivedType())
737 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
738
739 DIBasicType BTy(Ty);
740 if (BTy.isBasicType()) {
741 unsigned Encoding = BTy.getEncoding();
742 if (Encoding == dwarf::DW_ATE_unsigned ||
743 Encoding == dwarf::DW_ATE_unsigned_char ||
744 Encoding == dwarf::DW_ATE_boolean)
745 return true;
746 }
747 return false;
748}
749
750/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000751static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000752 unsigned Tag = Ty.getTag();
753
754 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
755 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
756 Tag != dwarf::DW_TAG_restrict_type)
757 return Ty.getSizeInBits();
758
759 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
760
761 // If this type is not derived from any type then take conservative approach.
762 if (!BaseType.isValid())
763 return Ty.getSizeInBits();
764
765 // If this is a derived type, go ahead and get the base type, unless it's a
766 // reference then it's just the size of the field. Pointer types have no need
767 // of this since they're a different type of qualification on the type.
768 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
769 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
770 return Ty.getSizeInBits();
771
772 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000773 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000774
775 return BaseType.getSizeInBits();
776}
777
Devang Patel0e821f42011-04-12 23:21:44 +0000778/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000779void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
780 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000781 // FIXME: This is a bit conservative/simple - it emits negative values at
782 // their maximum bit width which is a bit unfortunate (& doesn't prefer
783 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000784 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000785 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000786 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000787 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000788
Eric Christopher9d1daa82013-08-27 23:49:04 +0000789 // If we're a signed constant definitely use sdata.
790 if (SignedConstant) {
791 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
792 return;
793 }
794
795 // Else use data for now unless it's larger than we can deal with.
796 switch (SizeInBits) {
797 case 8:
798 Form = dwarf::DW_FORM_data1;
799 break;
800 case 16:
801 Form = dwarf::DW_FORM_data2;
802 break;
803 case 32:
804 Form = dwarf::DW_FORM_data4;
805 break;
806 case 64:
807 Form = dwarf::DW_FORM_data8;
808 break;
809 default:
810 Form = dwarf::DW_FORM_udata;
811 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
812 return;
813 }
814 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000815}
816
817/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000818void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000819 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000820 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
821 APFloat FPImm = MO.getFPImm()->getValueAPF();
822
823 // Get the raw data form of the floating point.
824 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000825 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000826
827 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000828 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000829 int Incr = (LittleEndian ? 1 : -1);
830 int Start = (LittleEndian ? 0 : NumBytes - 1);
831 int Stop = (LittleEndian ? NumBytes : -1);
832
833 // Output the constant to DWARF one byte at a time.
834 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000835 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000836
David Blaikief2443192013-10-21 17:28:37 +0000837 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000838}
839
David Blaikiea39a76e2013-01-20 01:18:01 +0000840/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000841void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000842 // Pass this down to addConstantValue as an unsigned bag of bits.
843 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000844}
845
Devang Patel0e821f42011-04-12 23:21:44 +0000846/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000847void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
848 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000849 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000850}
851
852// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000853void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000854 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000855 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000856 // If we're a signed constant definitely use sdata.
857 if (!Unsigned) {
858 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
859 Val.getSExtValue());
860 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000861 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000862
863 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000864 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000865 switch (CIBitWidth) {
866 case 8:
867 Form = dwarf::DW_FORM_data1;
868 break;
869 case 16:
870 Form = dwarf::DW_FORM_data2;
871 break;
872 case 32:
873 Form = dwarf::DW_FORM_data4;
874 break;
875 case 64:
876 Form = dwarf::DW_FORM_data8;
877 break;
878 default:
879 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
880 Val.getZExtValue());
881 return;
882 }
883 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000884 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000885 }
886
887 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
888
889 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000890 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000891
892 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000893 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000894
895 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000896 for (int i = 0; i < NumBytes; i++) {
897 uint8_t c;
898 if (LittleEndian)
899 c = Ptr64[i / 8] >> (8 * (i & 7));
900 else
901 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000902 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000903 }
Devang Patel0e821f42011-04-12 23:21:44 +0000904
David Blaikief2443192013-10-21 17:28:37 +0000905 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000906}
907
Eric Christopher25e35092013-04-22 07:47:40 +0000908/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000909void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000910 // Add template parameters.
911 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
912 DIDescriptor Element = TParams.getElement(i);
913 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000914 constructTemplateTypeParameterDIE(Buffer,
915 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000916 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000917 constructTemplateValueParameterDIE(Buffer,
918 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000919 }
Devang Patel0e821f42011-04-12 23:21:44 +0000920}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000921
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000922/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000923DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000924 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000925 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000926 if (Context.isType())
927 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000928 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000929 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000930 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000931 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000932 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000933}
934
Eric Christophera5a79422013-12-09 23:32:48 +0000935DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000936 DIScope Context = resolve(Ty.getContext());
937 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000938
939 DIE *TyDIE = getDIE(Ty);
940 if (TyDIE)
941 return TyDIE;
942
943 // Create new type.
944 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
945
David Blaikiefbd29eb2013-11-26 00:35:04 +0000946 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000947
David Blaikie9d861be2013-11-26 00:15:27 +0000948 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000949 return TyDIE;
950}
951
Devang Patel0e821f42011-04-12 23:21:44 +0000952/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
953/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000954DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000955 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000956 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000957
David Blaikie32887552013-11-14 22:25:02 +0000958 DIType Ty(TyNode);
959 assert(Ty.isType());
960
Manman Renf4c339e2013-10-29 22:49:29 +0000961 // Construct the context before querying for the existence of the DIE in case
962 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000963 DIScope Context = resolve(Ty.getContext());
964 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000965 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000966
Eric Christophere595bae2013-10-04 17:08:38 +0000967 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000968 if (TyDIE)
969 return TyDIE;
970
971 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000972 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
973
Devang Patel0e821f42011-04-12 23:21:44 +0000974 if (Ty.isBasicType())
975 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000976 else if (Ty.isCompositeType()) {
977 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000978 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
979 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikief645f962014-01-09 03:23:41 +0000980 DD->addDwarfTypeUnitType(getCUNode(), TypeId->getString(), TyDIE, CTy);
David Blaikiecfb21152014-01-03 18:59:42 +0000981 // Skip updating the accellerator tables since this is not the full type
982 return TyDIE;
983 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000984 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000985 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000986 assert(Ty.isDerivedType() && "Unknown kind of DIType");
987 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
988 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000989
David Blaikie9d861be2013-11-26 00:15:27 +0000990 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +0000991
992 return TyDIE;
993}
994
Eric Christophera5a79422013-12-09 23:32:48 +0000995void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
996 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000997 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
998 bool IsImplementation = 0;
999 if (Ty.isCompositeType()) {
1000 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001001 // A runtime language of 0 actually means C/C++ and that any
1002 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001003 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001004 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001005 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001006 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001007
1008 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1009 Context.isNameSpace())
1010 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001011 }
Devang Patel0e821f42011-04-12 23:21:44 +00001012}
1013
1014/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001015void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001016 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001017
1018 // Check for pre-existence.
1019 DIEEntry *Entry = getDIEEntry(Ty);
1020 // If it exists then use the existing value.
1021 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001022 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001023 return;
1024 }
1025
1026 // Construct type.
1027 DIE *Buffer = getOrCreateTypeDIE(Ty);
1028
1029 // Set up proxy.
1030 Entry = createDIEEntry(Buffer);
1031 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001032 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001033}
1034
Eric Christopher9cd26af2013-09-20 23:22:52 +00001035// Accelerator table mutators - add each name along with its companion
1036// DIE to the proper table while ensuring that the name that we're going
1037// to reference is in the string table. We do this since the names we
1038// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001039void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001040 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001041 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001042 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001043 DIEs.push_back(Die);
1044}
1045
Eric Christophera5a79422013-12-09 23:32:48 +00001046void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001047 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001048 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001049 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001050 DIEs.push_back(Die);
1051}
1052
Eric Christophera5a79422013-12-09 23:32:48 +00001053void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001054 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001055 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001056 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001057 DIEs.push_back(Die);
1058}
1059
Eric Christophera5a79422013-12-09 23:32:48 +00001060void DwarfUnit::addAccelType(StringRef Name,
1061 std::pair<const DIE *, unsigned> Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001062 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001063 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001064 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001065 DIEs.push_back(Die);
1066}
1067
Eric Christopher9c58f312013-09-20 22:20:55 +00001068/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001069void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001070 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001071 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001072}
1073
Eric Christopher2c8b7902013-10-17 02:06:06 +00001074/// getParentContextString - Walks the metadata parent chain in a language
1075/// specific manner (using the compile unit language) and returns
1076/// it as a string. This is done at the metadata level because DIEs may
1077/// not currently have been added to the parent context and walking the
1078/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001079std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001080 if (!Context)
1081 return "";
1082
1083 // FIXME: Decide whether to implement this for non-C++ languages.
1084 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1085 return "";
1086
Eric Christopher8dba0d52013-10-19 01:04:42 +00001087 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001088 SmallVector<DIScope, 1> Parents;
1089 while (!Context.isCompileUnit()) {
1090 Parents.push_back(Context);
1091 if (Context.getContext())
1092 Context = resolve(Context.getContext());
1093 else
1094 // Structure, etc types will have a NULL context if they're at the top
1095 // level.
1096 break;
1097 }
1098
1099 // Reverse iterate over our list to go from the outermost construct to the
1100 // innermost.
1101 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1102 E = Parents.rend();
1103 I != E; ++I) {
1104 DIScope Ctx = *I;
1105 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001106 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001107 CS += Name;
1108 CS += "::";
1109 }
1110 }
1111 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001112}
1113
1114/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001115void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001116 // Get core information.
1117 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001118 // Add name if not anonymous or intermediate type.
1119 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001120 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001121
David Blaikiefac56122013-10-04 23:21:16 +00001122 // An unspecified type only has a name attribute.
1123 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001124 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001125
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001126 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001127 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001128
Devang Patel0e821f42011-04-12 23:21:44 +00001129 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001130 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001131}
1132
1133/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001134void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001135 // Get core information.
1136 StringRef Name = DTy.getName();
1137 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001138 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001139
1140 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001141 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001142 if (FromTy)
1143 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001144
1145 // Add name if not anonymous or intermediate type.
1146 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001147 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001148
1149 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001150 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001151 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001152
David Blaikie5d3249b2013-01-07 05:51:15 +00001153 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001154 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1155 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001156 // Add source line info if available and TyDesc is not a forward declaration.
1157 if (!DTy.isForwardDecl())
1158 addSourceLine(&Buffer, DTy);
1159}
1160
1161/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001162void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001163 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001164 StringRef Name = CTy.getName();
1165
1166 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001167 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001168
1169 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001170 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001171 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001172 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001173 case dwarf::DW_TAG_enumeration_type:
1174 constructEnumTypeDIE(Buffer, CTy);
1175 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001176 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001177 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001178 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001179 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001180 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001181 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001182
1183 bool isPrototyped = true;
1184 // Add arguments.
1185 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1186 DIDescriptor Ty = Elements.getElement(i);
1187 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001188 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001189 isPrototyped = false;
1190 } else {
Manman Renb987e512013-10-29 00:53:03 +00001191 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001192 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001193 if (DIType(Ty).isArtificial())
1194 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001195 }
1196 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001197 // Add prototype flag if we're dealing with a C language and the
1198 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001199 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001200 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001201 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001202 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001203 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001204
1205 if (CTy.isLValueReference())
1206 addFlag(&Buffer, dwarf::DW_AT_reference);
1207
1208 if (CTy.isRValueReference())
1209 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001210 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001211 case dwarf::DW_TAG_structure_type:
1212 case dwarf::DW_TAG_union_type:
1213 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001214 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001215 DIArray Elements = CTy.getTypeArray();
1216 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001217 DIDescriptor Element = Elements.getElement(i);
1218 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001219 if (Element.isSubprogram())
1220 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1221 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001222 DIDerivedType DDTy(Element);
1223 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001224 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001225 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001226 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001227 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001228 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001229 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001230 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001231 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001232 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001233 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001234 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001235 StringRef PropertyName = Property.getObjCPropertyName();
1236 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001237 if (Property.getType())
1238 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001239 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001240 StringRef GetterName = Property.getObjCPropertyGetterName();
1241 if (!GetterName.empty())
1242 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1243 StringRef SetterName = Property.getObjCPropertySetterName();
1244 if (!SetterName.empty())
1245 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1246 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001247 if (Property.isReadOnlyObjCProperty())
1248 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1249 if (Property.isReadWriteObjCProperty())
1250 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1251 if (Property.isAssignObjCProperty())
1252 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1253 if (Property.isRetainObjCProperty())
1254 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1255 if (Property.isCopyObjCProperty())
1256 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1257 if (Property.isNonAtomicObjCProperty())
1258 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1259 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001260 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001261 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001262
Devang Pateld925d1a2012-02-07 23:33:58 +00001263 DIEEntry *Entry = getDIEEntry(Element);
1264 if (!Entry) {
1265 Entry = createDIEEntry(ElemDie);
1266 insertDIEEntry(Element, Entry);
1267 }
Devang Patel403e8192012-02-04 01:30:32 +00001268 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001269 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001270 }
1271
1272 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001273 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001274
Eric Christopher9e429ae2013-10-05 00:27:02 +00001275 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001276 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001277 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001278 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001279
Devang Patel12419ae2011-05-12 21:29:42 +00001280 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001281 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001282
Eric Christopherda011dd2011-12-16 23:42:42 +00001283 // Add template parameters to a class, structure or union types.
1284 // FIXME: The support isn't in the metadata for this yet.
1285 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001286 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001287 addTemplateParams(Buffer, CTy.getTemplateParams());
1288
1289 break;
1290 }
1291 default:
1292 break;
1293 }
1294
1295 // Add name if not anonymous or intermediate type.
1296 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001297 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001298
Eric Christopher775cbd22012-05-22 18:45:18 +00001299 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001300 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001301 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001302 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001303 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001304 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001305 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001306 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001307 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001308 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001309
1310 // If we're a forward decl, say so.
1311 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001312 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001313
1314 // Add source line info if available.
1315 if (!CTy.isForwardDecl())
1316 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001317
1318 // No harm in adding the runtime language to the declaration.
1319 unsigned RLang = CTy.getRunTimeLang();
1320 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001321 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1322 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001323 }
1324}
1325
Manman Renffc9a712013-10-23 23:05:28 +00001326/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1327/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001328void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1329 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001330 DIE *ParamDIE =
1331 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001332 // Add the type if it exists, it could be void and therefore no type.
1333 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001334 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001335 if (!TP.getName().empty())
1336 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001337}
1338
Manman Renffc9a712013-10-23 23:05:28 +00001339/// constructTemplateValueParameterDIE - Construct new DIE for the given
1340/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001341void
1342DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001343 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001344 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001345
1346 // Add the type if there is one, template template and template parameter
1347 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001348 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001349 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001350 if (!VP.getName().empty())
1351 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1352 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001353 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001354 addConstantValue(ParamDIE, CI,
1355 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001356 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1357 // For declaration non-type template parameters (such as global values and
1358 // functions)
1359 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001360 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001361 // Emit DW_OP_stack_value to use the address as the immediate value of the
1362 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001363 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1364 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001365 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001366 assert(isa<MDString>(Val));
1367 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1368 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001369 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001370 assert(isa<MDNode>(Val));
1371 DIArray A(cast<MDNode>(Val));
1372 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001373 }
1374 }
Devang Patel0e821f42011-04-12 23:21:44 +00001375}
1376
Devang Patel17b53272011-05-06 16:57:54 +00001377/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001378DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001379 // Construct the context before querying for the existence of the DIE in case
1380 // such construction creates the DIE.
1381 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001382
Devang Patel17b53272011-05-06 16:57:54 +00001383 DIE *NDie = getDIE(NS);
1384 if (NDie)
1385 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001386 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1387
Eric Christopher4996c702011-11-07 09:24:32 +00001388 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001389 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001390 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001391 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001392 } else
1393 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001394 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001395 return NDie;
1396}
1397
Devang Patel89543712011-08-15 17:24:54 +00001398/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001399DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001400 // Construct the context before querying for the existence of the DIE in case
1401 // such construction creates the DIE (as is the case for member function
1402 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001403 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001404
Eric Christophere595bae2013-10-04 17:08:38 +00001405 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001406 if (SPDie)
1407 return SPDie;
1408
Manman Ren73d697c2013-10-29 00:58:04 +00001409 DISubprogram SPDecl = SP.getFunctionDeclaration();
1410 if (SPDecl.isSubprogram())
1411 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001412 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001413
1414 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001415 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001416
Rafael Espindola79278362011-11-10 22:34:29 +00001417 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001418 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001419 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001420
Devang Patel89543712011-08-15 17:24:54 +00001421 // Add function template parameters.
1422 addTemplateParams(*SPDie, SP.getTemplateParams());
1423
Devang Patel89543712011-08-15 17:24:54 +00001424 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001425 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001426 if (DeclDie) {
1427 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001428 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001429
Devang Patel89543712011-08-15 17:24:54 +00001430 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001431 }
Devang Patel89543712011-08-15 17:24:54 +00001432
Eric Christopheracb71152012-08-23 22:52:55 +00001433 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001434 StringRef LinkageName = SP.getLinkageName();
1435 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001436 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001437 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001438
Devang Patel89543712011-08-15 17:24:54 +00001439 // Constructors and operators for anonymous aggregates do not have names.
1440 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001441 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001442
1443 addSourceLine(SPDie, SP);
1444
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001445 // Add the prototype if we have a prototype and we have a C like
1446 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001447 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001448 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001449 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001450 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001451 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001452
Devang Patel89543712011-08-15 17:24:54 +00001453 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001454 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1455 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001456
David Blaikie5174c842013-05-22 23:22:18 +00001457 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001458 // Add a return type. If this is a type like a C/C++ void type we don't add a
1459 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001460 if (Args.getElement(0))
1461 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001462
1463 unsigned VK = SP.getVirtuality();
1464 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001465 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001466 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001467 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1468 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1469 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001470 ContainingTypeMap.insert(
1471 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001472 }
1473
1474 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001475 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001476
Devang Patel89543712011-08-15 17:24:54 +00001477 // Add arguments. Do not add arguments for subprogram definition. They will
1478 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001479 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001480 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001481 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001482 addType(Arg, ATy);
1483 if (ATy.isArtificial())
1484 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001485 }
Devang Patel89543712011-08-15 17:24:54 +00001486 }
1487
1488 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001489 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001490
1491 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001492 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001493
1494 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001495 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001496
1497 if (unsigned isa = Asm->getISAEncoding()) {
1498 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1499 }
1500
Adrian Prantl99c7af22013-12-18 21:48:19 +00001501 if (SP.isLValueReference())
1502 addFlag(SPDie, dwarf::DW_AT_reference);
1503
1504 if (SP.isRValueReference())
1505 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1506
Adrian Prantlef129fb2014-01-18 02:12:00 +00001507 if (SP.isProtected())
1508 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1509 dwarf::DW_ACCESS_protected);
1510 else if (SP.isPrivate())
1511 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1512 dwarf::DW_ACCESS_private);
1513 else
1514 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1515 dwarf::DW_ACCESS_public);
1516
1517 if (SP.isExplicit())
1518 addFlag(SPDie, dwarf::DW_AT_explicit);
1519
Devang Patel89543712011-08-15 17:24:54 +00001520 return SPDie;
1521}
1522
Devang Pateldfd6ec32011-08-15 17:57:41 +00001523// Return const expression if value is a GEP to access merged global
1524// constant. e.g.
1525// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1526static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1527 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1528 if (!CE || CE->getNumOperands() != 3 ||
1529 CE->getOpcode() != Instruction::GetElementPtr)
1530 return NULL;
1531
1532 // First operand points to a global struct.
1533 Value *Ptr = CE->getOperand(0);
1534 if (!isa<GlobalValue>(Ptr) ||
1535 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1536 return NULL;
1537
1538 // Second operand is zero.
1539 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1540 if (!CI || !CI->isZero())
1541 return NULL;
1542
1543 // Third operand is offset.
1544 if (!isa<ConstantInt>(CE->getOperand(2)))
1545 return NULL;
1546
1547 return CE;
1548}
1549
1550/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001551void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001552 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001553 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001554 return;
1555
David Blaikie5e390e42014-02-04 01:23:52 +00001556 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001557
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001558 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001559 DIType GTy = GV.getType();
1560
1561 // If this is a static data member definition, some attributes belong
1562 // to the declaration DIE.
1563 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001564 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001565 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1566 if (SDMDecl.Verify()) {
1567 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1568 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001569 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001570 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001571 }
1572
1573 // If this is not a static data member definition, create the variable
1574 // DIE and add the initial set of attributes to it.
1575 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001576 // Construct the context before querying for the existence of the DIE in
1577 // case such construction creates the DIE.
1578 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001579
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001580 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001581 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001582
1583 // Add name and type.
1584 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1585 addType(VariableDIE, GTy);
1586
1587 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001588 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001589 addFlag(VariableDIE, dwarf::DW_AT_external);
1590
1591 // Add line number info.
1592 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001593 }
1594
Devang Pateldfd6ec32011-08-15 17:57:41 +00001595 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001596 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001597 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001598 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001599 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001600 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001601 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001602 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001603 if (GV.getGlobal()->isThreadLocal()) {
1604 // FIXME: Make this work with -gsplit-dwarf.
1605 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1606 assert((PointerSize == 4 || PointerSize == 8) &&
1607 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001608 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001609 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001610 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001611 if (!DD->useSplitDwarf()) {
1612 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001613 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001614 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001615 // 2) containing the (relocated) offset of the TLS variable
1616 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001617 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001618 } else {
David Blaikief2443192013-10-21 17:28:37 +00001619 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1620 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001621 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001622 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001623 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001624 } else {
1625 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001626 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001627 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001628 // Do not create specification DIE if context is either compile unit
1629 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001630 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001631 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001632 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001633 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001634 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001635 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001636 // A static member's declaration is already flagged as such.
1637 if (!SDMDecl.Verify())
1638 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001639 } else {
David Blaikief2443192013-10-21 17:28:37 +00001640 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001641 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001642 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001643 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001644 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001645 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1646 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1647 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001648 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1649 : VariableDIE,
1650 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001651 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001652 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001653 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001654 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001655 // emitting AT_const_value multiple times, we only add AT_const_value when
1656 // it is not a static member.
1657 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001658 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001659 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001660 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001661 // GV is a merged global.
1662 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1663 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001664 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1665 DD->addArangeLabel(SymbolCU(this, Sym));
1666 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001667 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001668 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001669 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001670 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001671 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1672 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001673 }
1674
Eric Christopherc12c2112011-11-11 01:55:22 +00001675 if (addToAccelTable) {
1676 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1677 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001678
Eric Christopherc12c2112011-11-11 01:55:22 +00001679 // If the linkage name is different than the name, go ahead and output
1680 // that as well into the name table.
1681 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1682 addAccelName(GV.getLinkageName(), AddrDIE);
1683 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001684
1685 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001686 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1687 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001688}
1689
Devang Patel0e821f42011-04-12 23:21:44 +00001690/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001691void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001692 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001693 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001694
Bill Wendling28fe9e72012-12-06 07:38:10 +00001695 // The LowerBound value defines the lower bounds which is typically zero for
1696 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1697 // Count == -1 then the array is unbounded and we do not emit
1698 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1699 // Count == 0, then the array has zero elements in which case we do not emit
1700 // an upper bound.
1701 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001702 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001703 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001704
Bill Wendling3495f9b2012-12-06 07:55:19 +00001705 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001706 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001707
1708 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001709 // FIXME: An unbounded array should reference the expression that defines
1710 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001711 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1712 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001713}
1714
1715/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001716void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001717 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001718 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001719
Eric Christopher0df08e22013-08-08 07:40:37 +00001720 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001721 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001722
1723 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001724 // FIXME: This type should be passed down from the front end
1725 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001726 DIE *IdxTy = getIndexTyDie();
1727 if (!IdxTy) {
1728 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001729 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001730 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001731 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001732 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1733 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001734 setIndexTyDie(IdxTy);
1735 }
1736
1737 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001738 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001739 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1740 DIDescriptor Element = Elements.getElement(i);
1741 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1742 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1743 }
1744}
1745
Eric Christopheraeb105f2013-11-11 18:52:39 +00001746/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001747void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001748 DIArray Elements = CTy.getTypeArray();
1749
1750 // Add enumerators to enumeration type.
1751 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001752 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001753 if (Enum.isEnumerator()) {
1754 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001755 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001756 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001757 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001758 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1759 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001760 }
1761 }
1762 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1763 if (DTy) {
1764 addType(&Buffer, DTy);
1765 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1766 }
Devang Patel0e821f42011-04-12 23:21:44 +00001767}
1768
Devang Patel89543712011-08-15 17:24:54 +00001769/// constructContainingTypeDIEs - Construct DIEs for types that contain
1770/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001771void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001772 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001773 CE = ContainingTypeMap.end();
1774 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001775 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001776 DIDescriptor D(CI->second);
1777 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001778 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001779 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001780 if (!NDie)
1781 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001782 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001783 }
1784}
1785
Devang Patel3acc70e2011-08-15 22:04:40 +00001786/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001787DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001788 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001789
Devang Patel3acc70e2011-08-15 22:04:40 +00001790 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001791 DIE *VariableDie = new DIE(DV.getTag());
1792 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001793 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001794 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001795 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001796 else {
David Blaikie715528b2013-08-19 03:34:03 +00001797 if (!Name.empty())
1798 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001799 addSourceLine(VariableDie, DV.getVariable());
1800 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001801 }
1802
Eric Christopher34a2c872013-11-15 01:43:19 +00001803 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001804 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001805
1806 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001807 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001808 return VariableDie;
1809 }
1810
1811 // Add variable address.
1812
Eric Christopher34a2c872013-11-15 01:43:19 +00001813 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001814 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001815 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1816 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001817 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001818 return VariableDie;
1819 }
1820
Eric Christophercead0332011-10-03 15:49:20 +00001821 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001822 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001823 assert(DVInsn->getNumOperands() == 3);
1824 if (DVInsn->getOperand(0).isReg()) {
1825 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001826 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001827 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001828 MachineLocation Location(RegOp.getReg(),
1829 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001830 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001831 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001832 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001833 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001834 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001835 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001836 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001837 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001838 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001839 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001840
Eric Christopher34a2c872013-11-15 01:43:19 +00001841 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001842 return VariableDie;
1843 } else {
1844 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001845 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001846 if (FI != ~0) {
1847 unsigned FrameReg = 0;
1848 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001849 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001850 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001851 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001852 }
1853 }
1854
Eric Christopher34a2c872013-11-15 01:43:19 +00001855 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001856 return VariableDie;
1857}
1858
Manman Ren230ec862013-10-23 23:00:44 +00001859/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001860void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001861 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001862 StringRef Name = DT.getName();
1863 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001864 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001865
Manman Ren93b30902013-10-08 18:42:58 +00001866 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001867
1868 addSourceLine(MemberDie, DT);
1869
Eric Christopherc2697f82013-10-19 01:04:47 +00001870 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001871
1872 // For C++, virtual base classes are not at fixed offset. Use following
1873 // expression to extract appropriate offset from vtable.
1874 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1875
1876 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001877 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1878 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1879 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1880 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1881 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1882 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1883 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001884
David Blaikief2443192013-10-21 17:28:37 +00001885 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001886 } else {
1887 uint64_t Size = DT.getSizeInBits();
1888 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1889 uint64_t OffsetInBytes;
1890
1891 if (Size != FieldSize) {
1892 // Handle bitfield.
1893 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1894 getBaseTypeSize(DD, DT) >> 3);
1895 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1896
1897 uint64_t Offset = DT.getOffsetInBits();
1898 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1899 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1900 uint64_t FieldOffset = (HiMark - FieldSize);
1901 Offset -= FieldOffset;
1902
1903 // Maybe we need to work from the other end.
1904 if (Asm->getDataLayout().isLittleEndian())
1905 Offset = FieldSize - (Offset + Size);
1906 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1907
Adrian Prantlc67655a2014-01-28 18:13:47 +00001908 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001909 // field that includes this bit field.
1910 OffsetInBytes = FieldOffset >> 3;
1911 } else
1912 // This is not a bitfield.
1913 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001914
David Blaikie22b29a52014-01-03 01:30:05 +00001915 if (DD->getDwarfVersion() <= 2) {
1916 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1917 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1918 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1919 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1920 } else
1921 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1922 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001923 }
Devang Patel0e821f42011-04-12 23:21:44 +00001924
1925 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001926 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001927 dwarf::DW_ACCESS_protected);
1928 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001929 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001930 dwarf::DW_ACCESS_private);
1931 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001932 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001933 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001934 dwarf::DW_ACCESS_public);
1935 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001936 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001937 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001938
1939 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001940 if (MDNode *PNode = DT.getObjCProperty())
1941 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001942 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001943 PropertyDie);
1944
David Blaikie37fefc32012-12-13 22:43:07 +00001945 if (DT.isArtificial())
1946 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001947}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001948
Manman Renc6b63922013-10-14 20:33:57 +00001949/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001950DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001951 if (!DT.Verify())
1952 return NULL;
1953
Manman Renc6b63922013-10-14 20:33:57 +00001954 // Construct the context before querying for the existence of the DIE in case
1955 // such construction creates the DIE.
1956 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001957 assert(dwarf::isType(ContextDIE->getTag()) &&
1958 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001959
1960 DIE *StaticMemberDIE = getDIE(DT);
1961 if (StaticMemberDIE)
1962 return StaticMemberDIE;
1963
Manman Renb987e512013-10-29 00:53:03 +00001964 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001965
Manman Ren93b30902013-10-08 18:42:58 +00001966 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001967
1968 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1969 addType(StaticMemberDIE, Ty);
1970 addSourceLine(StaticMemberDIE, DT);
1971 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1972 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1973
1974 // FIXME: We could omit private if the parent is a class_type, and
1975 // public if the parent is something else.
1976 if (DT.isProtected())
1977 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1978 dwarf::DW_ACCESS_protected);
1979 else if (DT.isPrivate())
1980 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1981 dwarf::DW_ACCESS_private);
1982 else
1983 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1984 dwarf::DW_ACCESS_public);
1985
1986 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001987 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001988 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1989 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001990
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001991 return StaticMemberDIE;
1992}
David Blaikie6b288cf2013-10-30 20:42:41 +00001993
Eric Christophera5a79422013-12-09 23:32:48 +00001994void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00001995 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001996 Asm->OutStreamer.AddComment("DWARF version number");
1997 Asm->EmitInt16(DD->getDwarfVersion());
1998 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001999 // We share one abbreviations table across all units so it's always at the
2000 // start of the section. Use a relocatable offset where needed to ensure
2001 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00002002 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00002003 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2004 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2005}
David Blaikiebc563272013-12-13 21:33:40 +00002006
Juergen Ributzkadb9ee002013-12-14 12:23:14 +00002007DwarfCompileUnit::~DwarfCompileUnit() {}
2008DwarfTypeUnit::~DwarfTypeUnit() {}
2009
David Blaikiebc563272013-12-13 21:33:40 +00002010void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2011 const MCSymbol *ASectionSym) const {
2012 DwarfUnit::emitHeader(ASection, ASectionSym);
2013 Asm->OutStreamer.AddComment("Type Signature");
2014 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2015 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002016 // In a skeleton type unit there is no type DIE so emit a zero offset.
2017 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2018 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002019}
2020
2021void DwarfTypeUnit::initSection(const MCSection *Section) {
2022 assert(!this->Section);
2023 this->Section = Section;
2024 // Since each type unit is contained in its own COMDAT section, the begin
2025 // label and the section label are the same. Using the begin label emission in
2026 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2027 // the only other alternative of lazily constructing start-of-section labels
2028 // and storing a mapping in DwarfDebug (or AsmPrinter).
2029 this->SectionSym = this->LabelBegin =
2030 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2031 this->LabelEnd =
2032 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2033 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2034}