blob: 49864ea1f3b541c1d7397b3599c649b5b9c25778 [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
David Blaikie2c86a722013-12-02 19:33:15 +000014#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000016#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/APFloat.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000019#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/GlobalVariable.h"
22#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000024#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000025#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000026#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000029#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000033
34using namespace llvm;
35
Chandler Carruth1b9dde02014-04-22 02:02:50 +000036#define DEBUG_TYPE "dwarfdebug"
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 Blaikiebd579052014-04-28 21:14:27 +000044DwarfUnit::DwarfUnit(unsigned UID, dwarf::Tag UnitTag, DICompileUnit Node,
David Blaikie92a2f8a2014-04-28 21:04:29 +000045 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
David Blaikiebd579052014-04-28 21:14:27 +000046 : UniqueID(UID), CUNode(Node), UnitDie(UnitTag), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr),
Craig Topper353eda42014-04-24 06:44:33 +000048 Skeleton(nullptr) {
David Blaikiebd579052014-04-28 21:14:27 +000049 assert(UnitTag == dwarf::DW_TAG_compile_unit ||
50 UnitTag == dwarf::DW_TAG_type_unit);
David Blaikie319a05f2013-12-02 19:33:10 +000051 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
52}
53
David Blaikiebd579052014-04-28 21:14:27 +000054DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node,
55 AsmPrinter *A, DwarfDebug *DW,
56 DwarfFile *DWU)
57 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) {
David Blaikie92a2f8a2014-04-28 21:04:29 +000058 insertDIE(Node, &getUnitDie());
Devang Patel0e821f42011-04-12 23:21:44 +000059}
60
David Blaikiebd579052014-04-28 21:14:27 +000061DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
David Blaikie92a2f8a2014-04-28 21:04:29 +000062 DwarfDebug *DW, DwarfFile *DWU,
David Blaikie8287aff2014-03-18 02:13:23 +000063 MCDwarfDwoLineTable *SplitLineTable)
David Blaikiebd579052014-04-28 21:14:27 +000064 : DwarfUnit(UID, dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU),
65 CU(CU), SplitLineTable(SplitLineTable) {
David Blaikie4a2f95f2014-03-18 01:17:26 +000066 if (SplitLineTable)
David Blaikiebd579052014-04-28 21:14:27 +000067 addSectionOffset(UnitDie, dwarf::DW_AT_stmt_list, 0);
David Blaikie4a2f95f2014-03-18 01:17:26 +000068}
David Blaikie409dd9c2013-11-19 23:08:21 +000069
David Blaikie319a05f2013-12-02 19:33:10 +000070/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000071DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000072 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
73 DIEBlocks[j]->~DIEBlock();
Eric Christopher4a741042014-02-16 08:46:55 +000074 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
75 DIELocs[j]->~DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +000076}
77
78/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
79/// information entry.
David Blaikie8dbcc3f2014-04-25 19:33:43 +000080DIEEntry *DwarfUnit::createDIEEntry(DIE &Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000081 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
82 return Value;
83}
84
Bill Wendling3495f9b2012-12-06 07:55:19 +000085/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000086/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000087int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000088 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000089 default:
90 break;
91
92 case dwarf::DW_LANG_C89:
93 case dwarf::DW_LANG_C99:
94 case dwarf::DW_LANG_C:
95 case dwarf::DW_LANG_C_plus_plus:
96 case dwarf::DW_LANG_ObjC:
97 case dwarf::DW_LANG_ObjC_plus_plus:
98 return 0;
99
100 case dwarf::DW_LANG_Fortran77:
101 case dwarf::DW_LANG_Fortran90:
102 case dwarf::DW_LANG_Fortran95:
103 return 1;
104
105 // The languages below have valid values only if the DWARF version >= 4.
106 case dwarf::DW_LANG_Java:
107 case dwarf::DW_LANG_Python:
108 case dwarf::DW_LANG_UPC:
109 case dwarf::DW_LANG_D:
110 if (dwarf::DWARF_VERSION >= 4)
111 return 0;
112 break;
113
114 case dwarf::DW_LANG_Ada83:
115 case dwarf::DW_LANG_Ada95:
116 case dwarf::DW_LANG_Cobol74:
117 case dwarf::DW_LANG_Cobol85:
118 case dwarf::DW_LANG_Modula2:
119 case dwarf::DW_LANG_Pascal83:
120 case dwarf::DW_LANG_PLI:
121 if (dwarf::DWARF_VERSION >= 4)
122 return 1;
123 break;
124 }
125
126 return -1;
127}
128
Manman Ren4dbdc902013-10-31 17:54:35 +0000129/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000130static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000131 // When the MDNode can be part of the type system, the DIE can be shared
132 // across CUs.
133 // Combining type units and cross-CU DIE sharing is lower value (since
134 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
135 // level already) but may be implementable for some value in projects
136 // building multiple independent libraries with LTO and then linking those
137 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000138 return (D.isType() ||
139 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000140 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000141}
142
143/// getDIE - Returns the debug information entry map slot for the
144/// specified debug variable. We delegate the request to DwarfDebug
145/// when the DIE for this MDNode can be shared across CUs. The mappings
146/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000147DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000148 if (isShareableAcrossCUs(D))
149 return DD->getDIE(D);
150 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000151}
152
153/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
154/// when the DIE for this MDNode can be shared across CUs. The mappings
155/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000156void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000157 if (isShareableAcrossCUs(Desc)) {
158 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000159 return;
160 }
David Blaikie2ad00162013-11-15 23:09:13 +0000161 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000162}
163
Eric Christopherbb69a272012-08-24 01:14:27 +0000164/// addFlag - Add a flag that is true.
David Blaikie65a74662014-04-25 18:26:14 +0000165void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000166 if (DD->getDwarfVersion() >= 4)
David Blaikie65a74662014-04-25 18:26:14 +0000167 Die.addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000168 else
David Blaikie65a74662014-04-25 18:26:14 +0000169 Die.addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000170}
171
Devang Patel0e821f42011-04-12 23:21:44 +0000172/// addUInt - Add an unsigned integer attribute data and value.
173///
David Blaikie65a74662014-04-25 18:26:14 +0000174void DwarfUnit::addUInt(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000175 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000176 if (!Form)
177 Form = DIEInteger::BestForm(false, Integer);
178 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
179 DIEInteger(Integer);
David Blaikie65a74662014-04-25 18:26:14 +0000180 Die.addValue(Attribute, *Form, Value);
David Blaikief2443192013-10-21 17:28:37 +0000181}
182
David Blaikie65a74662014-04-25 18:26:14 +0000183void DwarfUnit::addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000184 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000185}
186
187/// addSInt - Add an signed integer attribute data and value.
188///
David Blaikie65a74662014-04-25 18:26:14 +0000189void DwarfUnit::addSInt(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000190 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000191 if (!Form)
192 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000193 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikie65a74662014-04-25 18:26:14 +0000194 Die.addValue(Attribute, *Form, Value);
David Blaikief2443192013-10-21 17:28:37 +0000195}
196
David Blaikie65a74662014-04-25 18:26:14 +0000197void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000198 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000199 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000200}
201
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000202/// addString - Add a string attribute data and value. We always emit a
203/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000204/// more predictable sizes. In the case of split dwarf we emit an index
205/// into another table which gets us the static offset into the string
206/// table.
David Blaikie65a74662014-04-25 18:26:14 +0000207void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000208 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000209
210 if (!DD->useSplitDwarf())
211 return addLocalString(Die, Attribute, String);
212
David Blaikiedaefdbf2014-04-25 21:34:35 +0000213 unsigned idx = DU->getStringPool().getIndex(*Asm, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000214 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000215 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
David Blaikie65a74662014-04-25 18:26:14 +0000216 Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000217}
218
219/// addLocalString - Add a string attribute data and value. This is guaranteed
220/// to be in the local string pool instead of indirected.
David Blaikie65a74662014-04-25 18:26:14 +0000221void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000222 StringRef String) {
David Blaikiedaefdbf2014-04-25 21:34:35 +0000223 MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000224 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000225 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000226 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000227 else {
David Blaikiedaefdbf2014-04-25 21:34:35 +0000228 MCSymbol *StringPool = DU->getStringPool().getSectionSymbol();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000229 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000230 }
Eric Christopher05893f42013-12-30 18:32:31 +0000231 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
David Blaikie65a74662014-04-25 18:26:14 +0000232 Die.addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000233}
234
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000236///
David Blaikie65a74662014-04-25 18:26:14 +0000237void DwarfUnit::addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000238 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikie65a74662014-04-25 18:26:14 +0000239 Die.addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000240}
241
Eric Christophera27220f2014-03-05 22:41:20 +0000242/// addLocationList - Add a Dwarf loclistptr attribute data and value.
243///
David Blaikie65a74662014-04-25 18:26:14 +0000244void DwarfUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera27220f2014-03-05 22:41:20 +0000245 unsigned Index) {
246 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index);
247 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
248 : dwarf::DW_FORM_data4;
David Blaikie65a74662014-04-25 18:26:14 +0000249 Die.addValue(Attribute, Form, Value);
Eric Christophera27220f2014-03-05 22:41:20 +0000250}
251
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000252/// addLabel - Add a Dwarf label attribute data and value.
253///
David Blaikie65a74662014-04-25 18:26:14 +0000254void DwarfUnit::addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000255 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000256 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
David Blaikie65a74662014-04-25 18:26:14 +0000257 Die.addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000258}
259
David Blaikie65a74662014-04-25 18:26:14 +0000260void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000261 addLabel(Die, (dwarf::Attribute)0, Form, Label);
262}
263
Eric Christopher33ff6972013-11-21 23:46:41 +0000264/// addSectionLabel - Add a Dwarf section label attribute data and value.
265///
David Blaikie65a74662014-04-25 18:26:14 +0000266void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000267 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000268 if (DD->getDwarfVersion() >= 4)
269 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
270 else
271 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
272}
273
274/// addSectionOffset - Add an offset into a section attribute data and value.
275///
David Blaikie65a74662014-04-25 18:26:14 +0000276void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000277 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000278 if (DD->getDwarfVersion() >= 4)
279 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
280 else
281 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
282}
283
Eric Christopher962c9082013-01-15 23:56:56 +0000284/// addLabelAddress - Add a dwarf label attribute data and value using
285/// DW_FORM_addr or DW_FORM_GNU_addr_index.
286///
David Blaikie65a74662014-04-25 18:26:14 +0000287void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000288 const MCSymbol *Label) {
289
290 if (!DD->useSplitDwarf())
291 return addLocalLabelAddress(Die, Attribute, Label);
292
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000293 if (Label)
294 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000295
David Blaikied75fb282014-04-23 21:20:10 +0000296 unsigned idx = DD->getAddressPool().getIndex(Label);
Eric Christopher384f3fe2014-03-20 19:16:16 +0000297 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
David Blaikie65a74662014-04-25 18:26:14 +0000298 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher384f3fe2014-03-20 19:16:16 +0000299}
300
David Blaikie65a74662014-04-25 18:26:14 +0000301void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000302 dwarf::Attribute Attribute,
303 const MCSymbol *Label) {
304 if (Label)
305 DD->addArangeLabel(SymbolCU(this, Label));
306
David Blaikie65a74662014-04-25 18:26:14 +0000307 Die.addValue(Attribute, dwarf::DW_FORM_addr,
308 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
309 : new (DIEValueAllocator) DIEInteger(0));
Eric Christopher962c9082013-01-15 23:56:56 +0000310}
311
David Blaikie0e8d4012014-03-17 23:53:25 +0000312unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
313 // If we print assembly, we can't separate .file entries according to
314 // compile units. Thus all files will belong to the default compile unit.
315
316 // FIXME: add a better feature test than hasRawTextSupport. Even better,
317 // extend .file to support this.
318 return Asm->OutStreamer.EmitDwarfFileDirective(
319 0, DirName, FileName,
320 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
321}
322
David Blaikie4a2f95f2014-03-18 01:17:26 +0000323unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
324 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
325 : getCU().getOrCreateSourceID(FileName, DirName);
326}
327
Eric Christophere9ec2452013-01-18 22:11:33 +0000328/// addOpAddress - Add a dwarf op address data and value using the
329/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
330///
David Blaikie65a74662014-04-25 18:26:14 +0000331void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000332 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000333 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
334 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000335 } else {
David Blaikief2443192013-10-21 17:28:37 +0000336 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
David Blaikiee226b082014-04-23 21:04:59 +0000337 addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
David Blaikied75fb282014-04-23 21:20:10 +0000338 DD->getAddressPool().getIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000339 }
340}
341
Eric Christopher33ff6972013-11-21 23:46:41 +0000342/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000343///
David Blaikie65a74662014-04-25 18:26:14 +0000344void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000345 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000346 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
David Blaikie65a74662014-04-25 18:26:14 +0000347 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
348 : dwarf::DW_FORM_data4,
349 Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000350}
351
David Blaikie65a74662014-04-25 18:26:14 +0000352void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
David Blaikie48b1bdc2014-03-07 01:30:55 +0000353 const MCSymbol *Hi, const MCSymbol *Lo) {
354 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
David Blaikie65a74662014-04-25 18:26:14 +0000355 Die.addValue(Attribute, dwarf::DW_FORM_data4, Value);
David Blaikie48b1bdc2014-03-07 01:30:55 +0000356}
357
Devang Patel0e821f42011-04-12 23:21:44 +0000358/// addDIEEntry - Add a DIE attribute data and value.
359///
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000360void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000361 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
362}
363
David Blaikie65a74662014-04-25 18:26:14 +0000364void DwarfUnit::addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type) {
David Blaikief3de2ab2014-04-26 16:26:41 +0000365 // Flag the type unit reference as a declaration so that if it contains
366 // members (implicit special members, static data member definitions, member
367 // declarations for definitions in this CU, etc) consumers don't get confused
368 // and think this is a full definition.
369 addFlag(Die, dwarf::DW_AT_declaration);
370
David Blaikie65a74662014-04-25 18:26:14 +0000371 Die.addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
372 new (DIEValueAllocator) DIETypeSignature(Type));
David Blaikie47f615e2013-12-17 23:32:35 +0000373}
374
David Blaikie65a74662014-04-25 18:26:14 +0000375void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000376 DIEEntry *Entry) {
David Blaikie65a74662014-04-25 18:26:14 +0000377 const DIE *DieCU = Die.getUnitOrNull();
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000378 const DIE *EntryCU = Entry->getEntry().getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000379 if (!DieCU)
380 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikieadcde362014-04-25 18:35:57 +0000381 DieCU = &getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000382 if (!EntryCU)
David Blaikieadcde362014-04-25 18:35:57 +0000383 EntryCU = &getUnitDie();
David Blaikie65a74662014-04-25 18:26:14 +0000384 Die.addValue(Attribute,
385 EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
386 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000387}
388
Manman Renb987e512013-10-29 00:53:03 +0000389/// Create a DIE with the given Tag, add the DIE to its parent, and
390/// call insertDIE if MD is not null.
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000391DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
David Blaikieefc403b2014-04-12 02:24:04 +0000392 assert(Tag != dwarf::DW_TAG_auto_variable &&
393 Tag != dwarf::DW_TAG_arg_variable);
David Blaikie914046e2014-04-25 20:00:34 +0000394 Parent.addChild(make_unique<DIE>((dwarf::Tag)Tag));
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000395 DIE &Die = *Parent.getChildren().back();
David Blaikie52c50202013-11-16 00:29:01 +0000396 if (N)
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000397 insertDIE(N, &Die);
Manman Renb987e512013-10-29 00:53:03 +0000398 return Die;
399}
400
Devang Patel0e821f42011-04-12 23:21:44 +0000401/// addBlock - Add block data.
402///
David Blaikie65a74662014-04-25 18:26:14 +0000403void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000404 Loc->ComputeSize(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000405 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
David Blaikie65a74662014-04-25 18:26:14 +0000406 Die.addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
Eric Christopher4a741042014-02-16 08:46:55 +0000407}
408
David Blaikie65a74662014-04-25 18:26:14 +0000409void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000410 DIEBlock *Block) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000411 Block->ComputeSize(Asm);
Devang Patel0e821f42011-04-12 23:21:44 +0000412 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
David Blaikie65a74662014-04-25 18:26:14 +0000413 Die.addValue(Attribute, Block->BestForm(), Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000414}
415
416/// addSourceLine - Add location information to specified debug information
417/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000418void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File,
David Blaikie101613e2014-02-12 00:11:25 +0000419 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000420 if (Line == 0)
421 return;
David Blaikie101613e2014-02-12 00:11:25 +0000422
David Blaikie4a2f95f2014-03-18 01:17:26 +0000423 unsigned FileID = getOrCreateSourceID(File, Directory);
Devang Patel0e821f42011-04-12 23:21:44 +0000424 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000425 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
426 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000427}
428
429/// addSourceLine - Add location information to specified debug information
430/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000431void DwarfUnit::addSourceLine(DIE &Die, DIVariable V) {
David Blaikie101613e2014-02-12 00:11:25 +0000432 assert(V.isVariable());
433
Eric Christopher89a575c2014-02-12 22:38:04 +0000434 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
435 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000436}
437
438/// addSourceLine - Add location information to specified debug information
439/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000440void DwarfUnit::addSourceLine(DIE &Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000441 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000442
David Blaikie101613e2014-02-12 00:11:25 +0000443 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000444}
445
446/// addSourceLine - Add location information to specified debug information
447/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000448void DwarfUnit::addSourceLine(DIE &Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000449 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000450
David Blaikie101613e2014-02-12 00:11:25 +0000451 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000452}
453
454/// addSourceLine - Add location information to specified debug information
455/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000456void DwarfUnit::addSourceLine(DIE &Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000457 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000458
David Blaikie101613e2014-02-12 00:11:25 +0000459 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000460}
461
462/// addSourceLine - Add location information to specified debug information
463/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000464void DwarfUnit::addSourceLine(DIE &Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000465 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000466
Eric Christopher70e1bd82012-03-29 08:42:56 +0000467 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000468 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
469 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000470}
471
472/// addSourceLine - Add location information to specified debug information
473/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000474void DwarfUnit::addSourceLine(DIE &Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000475 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000476
David Blaikie101613e2014-02-12 00:11:25 +0000477 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000478}
479
Eric Christopher92331fd2012-11-21 00:34:38 +0000480/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000481/// DbgVariable based on provided MachineLocation.
David Blaikie65a74662014-04-25 18:26:14 +0000482void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera5a79422013-12-09 23:32:48 +0000483 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000484 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000485 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000486 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000487 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
488 else
David Blaikieea2605d2013-06-20 00:25:24 +0000489 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000490 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000491}
492
Devang Patelba5fbf12011-04-26 19:06:18 +0000493/// addRegisterOp - Add register operand.
David Blaikie65a74662014-04-25 18:26:14 +0000494void DwarfUnit::addRegisterOp(DIELoc &TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000495 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000496 int DWReg = RI->getDwarfRegNum(Reg, false);
497 bool isSubRegister = DWReg < 0;
498
499 unsigned Idx = 0;
500
501 // Go up the super-register chain until we hit a valid dwarf register number.
502 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
503 DWReg = RI->getDwarfRegNum(*SR, false);
504 if (DWReg >= 0)
505 Idx = RI->getSubRegIndex(*SR, Reg);
506 }
507
508 if (DWReg < 0) {
Eric Christophera13839f2014-02-26 23:27:16 +0000509 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000510 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
511 return;
512 }
513
514 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000515 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000516 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000517 else {
David Blaikief2443192013-10-21 17:28:37 +0000518 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
519 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000520 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000521
522 // Emit Mask
523 if (isSubRegister) {
524 unsigned Size = RI->getSubRegIdxSize(Idx);
525 unsigned Offset = RI->getSubRegIdxOffset(Idx);
526 if (Offset > 0) {
527 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
528 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
529 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
530 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000531 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000532 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000533 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000534 }
535 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000536}
537
538/// addRegisterOffset - Add register offset.
David Blaikie65a74662014-04-25 18:26:14 +0000539void DwarfUnit::addRegisterOffset(DIELoc &TheDie, unsigned Reg,
Eric Christophera5a79422013-12-09 23:32:48 +0000540 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000541 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
542 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
543 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
544 if (Reg == TRI->getFrameRegister(*Asm->MF))
545 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000546 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000547 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000548 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000549 else {
David Blaikief2443192013-10-21 17:28:37 +0000550 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
551 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000552 }
David Blaikief2443192013-10-21 17:28:37 +0000553 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000554}
555
556/// addAddress - Add an address attribute to a die based on the location
557/// provided.
David Blaikie65a74662014-04-25 18:26:14 +0000558void DwarfUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000559 const MachineLocation &Location, bool Indirect) {
Eric Christopher4a741042014-02-16 08:46:55 +0000560 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patelba5fbf12011-04-26 19:06:18 +0000561
David Blaikieea2605d2013-06-20 00:25:24 +0000562 if (Location.isReg() && !Indirect)
David Blaikie65a74662014-04-25 18:26:14 +0000563 addRegisterOp(*Loc, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000564 else {
David Blaikie65a74662014-04-25 18:26:14 +0000565 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000566 if (Indirect && !Location.isReg()) {
David Blaikie65a74662014-04-25 18:26:14 +0000567 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000568 }
569 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000570
571 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000572 addBlock(Die, Attribute, Loc);
Devang Patelba5fbf12011-04-26 19:06:18 +0000573}
574
Devang Patel0e821f42011-04-12 23:21:44 +0000575/// addComplexAddress - Start with the address based on the location provided,
576/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000577/// given the extra address information encoded in the DbgVariable, starting
578/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000579///
David Blaikie65a74662014-04-25 18:26:14 +0000580void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera5a79422013-12-09 23:32:48 +0000581 dwarf::Attribute Attribute,
582 const MachineLocation &Location) {
Eric Christopher4a741042014-02-16 08:46:55 +0000583 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000584 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000585 unsigned i = 0;
586 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000587 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000588 // If first address element is OpPlus then emit
589 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
David Blaikie65a74662014-04-25 18:26:14 +0000590 addRegisterOffset(*Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000591 i = 2;
592 } else
David Blaikie65a74662014-04-25 18:26:14 +0000593 addRegisterOp(*Loc, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000594 } else
David Blaikie65a74662014-04-25 18:26:14 +0000595 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000596
Eric Christopherc2697f82013-10-19 01:04:47 +0000597 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000598 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000599 if (Element == DIBuilder::OpPlus) {
David Blaikie65a74662014-04-25 18:26:14 +0000600 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
601 addUInt(*Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000602 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000603 if (!Location.isReg())
David Blaikie65a74662014-04-25 18:26:14 +0000604 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000605 } else
606 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000607 }
608
609 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000610 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000611}
612
613/* Byref variables, in Blocks, are declared by the programmer as "SomeType
614 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
615 gives the variable VarName either the struct, or a pointer to the struct, as
616 its type. This is necessary for various behind-the-scenes things the
617 compiler needs to do with by-reference variables in Blocks.
618
619 However, as far as the original *programmer* is concerned, the variable
620 should still have type 'SomeType', as originally declared.
621
622 The function getBlockByrefType dives into the __Block_byref_x_VarName
623 struct to find the original type of the variable, which is then assigned to
624 the variable's Debug Information Entry as its real type. So far, so good.
625 However now the debugger will expect the variable VarName to have the type
626 SomeType. So we need the location attribute for the variable to be an
627 expression that explains to the debugger how to navigate through the
628 pointers and struct to find the actual variable of type SomeType.
629
630 The following function does just that. We start by getting
631 the "normal" location for the variable. This will be the location
632 of either the struct __Block_byref_x_VarName or the pointer to the
633 struct __Block_byref_x_VarName.
634
635 The struct will look something like:
636
637 struct __Block_byref_x_VarName {
638 ... <various fields>
639 struct __Block_byref_x_VarName *forwarding;
640 ... <various other fields>
641 SomeType VarName;
642 ... <maybe more fields>
643 };
644
645 If we are given the struct directly (as our starting point) we
646 need to tell the debugger to:
647
648 1). Add the offset of the forwarding field.
649
650 2). Follow that pointer to get the real __Block_byref_x_VarName
651 struct to use (the real one may have been copied onto the heap).
652
653 3). Add the offset for the field VarName, to find the actual variable.
654
655 If we started with a pointer to the struct, then we need to
656 dereference that pointer first, before the other steps.
657 Translating this into DWARF ops, we will need to append the following
658 to the current location description for the variable:
659
660 DW_OP_deref -- optional, if we start with a pointer
661 DW_OP_plus_uconst <forward_fld_offset>
662 DW_OP_deref
663 DW_OP_plus_uconst <varName_fld_offset>
664
665 That is what this function does. */
666
667/// addBlockByrefAddress - Start with the address based on the location
668/// provided, and generate the DWARF information necessary to find the
669/// actual Block variable (navigating the Block struct) based on the
670/// starting location. Add the DWARF information to the die. For
671/// more information, read large comment just above here.
672///
David Blaikie65a74662014-04-25 18:26:14 +0000673void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera5a79422013-12-09 23:32:48 +0000674 dwarf::Attribute Attribute,
675 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000676 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000677 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000678 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000679 bool isPointer = false;
680
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000681 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000682
683 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000684 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000685 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000686 isPointer = true;
687 }
688
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000689 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000690
691 // Find the __forwarding field and the variable field in the __Block_byref
692 // struct.
693 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000694 DIDerivedType varField;
695 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000696
697 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000698 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000699 StringRef fieldName = DT.getName();
700 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000701 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000702 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000703 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000704 }
705
706 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000707 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
708 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000709
710 // Decode the original location, and use that as the start of the byref
711 // variable's location.
Eric Christopher4a741042014-02-16 08:46:55 +0000712 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +0000713
Eric Christopheref9d7102012-07-04 02:02:18 +0000714 if (Location.isReg())
David Blaikie65a74662014-04-25 18:26:14 +0000715 addRegisterOp(*Loc, Location.getReg());
Eric Christopheref9d7102012-07-04 02:02:18 +0000716 else
David Blaikie65a74662014-04-25 18:26:14 +0000717 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000718
719 // If we started with a pointer to the __Block_byref... struct, then
720 // the first thing we need to do is dereference the pointer (DW_OP_deref).
721 if (isPointer)
David Blaikie65a74662014-04-25 18:26:14 +0000722 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000723
724 // Next add the offset for the '__forwarding' field:
725 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
726 // adding the offset if it's 0.
727 if (forwardingFieldOffset > 0) {
David Blaikie65a74662014-04-25 18:26:14 +0000728 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
729 addUInt(*Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000730 }
731
732 // Now dereference the __forwarding field to get to the real __Block_byref
733 // struct: DW_OP_deref.
David Blaikie65a74662014-04-25 18:26:14 +0000734 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000735
736 // Now that we've got the real __Block_byref... struct, add the offset
737 // for the variable's field to get to the location of the actual variable:
738 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
739 if (varFieldOffset > 0) {
David Blaikie65a74662014-04-25 18:26:14 +0000740 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
741 addUInt(*Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000742 }
743
744 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000745 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000746}
747
Manman Renb3388602013-10-05 01:43:03 +0000748/// Return true if type encoding is unsigned.
749static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
750 DIDerivedType DTy(Ty);
David Blaikiee0f14742014-05-11 17:04:05 +0000751 if (DTy.isDerivedType()) {
David Blaikie2af1c802014-05-20 18:21:51 +0000752 dwarf::Tag T = (dwarf::Tag)Ty.getTag();
753 // Encode pointer constants as unsigned bytes. This is used at least for
David Blaikie93ef46b2014-05-20 21:40:13 +0000754 // null pointer constant emission.
755 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
756 // here, but accept them for now due to a bug in SROA producing bogus
757 // dbg.values.
David Blaikie2af1c802014-05-20 18:21:51 +0000758 if (T == dwarf::DW_TAG_pointer_type ||
David Blaikie93ef46b2014-05-20 21:40:13 +0000759 T == dwarf::DW_TAG_ptr_to_member_type ||
760 T == dwarf::DW_TAG_reference_type ||
761 T == dwarf::DW_TAG_rvalue_reference_type)
David Blaikie2af1c802014-05-20 18:21:51 +0000762 return true;
763 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
764 T == dwarf::DW_TAG_volatile_type ||
David Blaikie1d9aec62014-05-20 18:36:35 +0000765 T == dwarf::DW_TAG_restrict_type ||
766 T == dwarf::DW_TAG_enumeration_type);
David Blaikie2af1c802014-05-20 18:21:51 +0000767 if (DITypeRef Deriv = DTy.getTypeDerivedFrom())
768 return isUnsignedDIType(DD, DD->resolve(Deriv));
David Blaikiee0f14742014-05-11 17:04:05 +0000769 // FIXME: Enums without a fixed underlying type have unknown signedness
770 // here, leading to incorrectly emitted constants.
771 assert(DTy.getTag() == dwarf::DW_TAG_enumeration_type);
772 return false;
773 }
Manman Renb3388602013-10-05 01:43:03 +0000774
775 DIBasicType BTy(Ty);
David Blaikiee0f14742014-05-11 17:04:05 +0000776 assert(BTy.isBasicType());
David Blaikie60cae1b2014-05-11 16:08:41 +0000777 unsigned Encoding = BTy.getEncoding();
Saleem Abdulrasoolfba09d42014-05-12 06:08:18 +0000778 assert((Encoding == dwarf::DW_ATE_unsigned ||
779 Encoding == dwarf::DW_ATE_unsigned_char ||
780 Encoding == dwarf::DW_ATE_signed ||
781 Encoding == dwarf::DW_ATE_signed_char ||
David Blaikiec405c9c2014-05-16 21:53:09 +0000782 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean) &&
783 "Unsupported encoding");
David Blaikie60cae1b2014-05-11 16:08:41 +0000784 return (Encoding == dwarf::DW_ATE_unsigned ||
785 Encoding == dwarf::DW_ATE_unsigned_char ||
David Blaikiec405c9c2014-05-16 21:53:09 +0000786 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean);
Manman Renb3388602013-10-05 01:43:03 +0000787}
788
789/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000790static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000791 unsigned Tag = Ty.getTag();
792
793 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
794 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
795 Tag != dwarf::DW_TAG_restrict_type)
796 return Ty.getSizeInBits();
797
798 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
799
Eric Christopher8cc04fc2014-03-12 18:18:05 +0000800 // If this type is not derived from any type or the type is a declaration then
801 // take conservative approach.
802 if (!BaseType.isValid() || BaseType.isForwardDecl())
Manman Renb3388602013-10-05 01:43:03 +0000803 return Ty.getSizeInBits();
804
805 // If this is a derived type, go ahead and get the base type, unless it's a
806 // reference then it's just the size of the field. Pointer types have no need
807 // of this since they're a different type of qualification on the type.
808 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
809 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
810 return Ty.getSizeInBits();
811
812 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000813 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000814
815 return BaseType.getSizeInBits();
816}
817
Devang Patel0e821f42011-04-12 23:21:44 +0000818/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000819void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000820 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000821 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
822 APFloat FPImm = MO.getFPImm()->getValueAPF();
823
824 // Get the raw data form of the floating point.
825 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000826 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000827
828 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000829 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000830 int Incr = (LittleEndian ? 1 : -1);
831 int Start = (LittleEndian ? 0 : NumBytes - 1);
832 int Stop = (LittleEndian ? NumBytes : -1);
833
834 // Output the constant to DWARF one byte at a time.
835 for (; Start != Stop; Start += Incr)
David Blaikie65a74662014-04-25 18:26:14 +0000836 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000837
David Blaikief2443192013-10-21 17:28:37 +0000838 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000839}
840
David Blaikiea39a76e2013-01-20 01:18:01 +0000841/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000842void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000843 // Pass this down to addConstantValue as an unsigned bag of bits.
844 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000845}
846
Devang Patel0e821f42011-04-12 23:21:44 +0000847/// addConstantValue - Add constant value entry in variable DIE.
David Blaikiec0a28412014-05-11 15:56:59 +0000848void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, DIType Ty) {
849 addConstantValue(Die, CI->getValue(), Ty);
David Blaikiea39a76e2013-01-20 01:18:01 +0000850}
851
David Blaikiec05c8f42014-05-11 15:47:39 +0000852/// addConstantValue - Add constant value entry in variable DIE.
853void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO,
854 DIType Ty) {
855 assert(MO.isImm() && "Invalid machine operand!");
856
David Blaikie60cae1b2014-05-11 16:08:41 +0000857 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm());
David Blaikiec05c8f42014-05-11 15:47:39 +0000858}
859
David Blaikie60cae1b2014-05-11 16:08:41 +0000860void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
David Blaikiec05c8f42014-05-11 15:47:39 +0000861 // FIXME: This is a bit conservative/simple - it emits negative values always
862 // sign extended to 64 bits rather than minimizing the number of bytes.
863 addUInt(Die, dwarf::DW_AT_const_value,
David Blaikie60cae1b2014-05-11 16:08:41 +0000864 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
David Blaikiec05c8f42014-05-11 15:47:39 +0000865}
866
David Blaikiec0a28412014-05-11 15:56:59 +0000867void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, DIType Ty) {
868 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty));
869}
870
David Blaikiea39a76e2013-01-20 01:18:01 +0000871// addConstantValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000872void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000873 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000874 if (CIBitWidth <= 64) {
David Blaikie60cae1b2014-05-11 16:08:41 +0000875 addConstantValue(Die, Unsigned,
876 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000877 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000878 }
879
880 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
881
882 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000883 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000884
885 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000886 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000887
888 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000889 for (int i = 0; i < NumBytes; i++) {
890 uint8_t c;
891 if (LittleEndian)
892 c = Ptr64[i / 8] >> (8 * (i & 7));
893 else
894 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikie65a74662014-04-25 18:26:14 +0000895 addUInt(*Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000896 }
Devang Patel0e821f42011-04-12 23:21:44 +0000897
David Blaikief2443192013-10-21 17:28:37 +0000898 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000899}
900
Eric Christopher25e35092013-04-22 07:47:40 +0000901/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000902void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000903 // Add template parameters.
904 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
905 DIDescriptor Element = TParams.getElement(i);
906 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000907 constructTemplateTypeParameterDIE(Buffer,
908 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000909 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000910 constructTemplateValueParameterDIE(Buffer,
911 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000912 }
Devang Patel0e821f42011-04-12 23:21:44 +0000913}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000914
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000915/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000916DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000917 if (!Context || Context.isFile())
David Blaikieadcde362014-04-25 18:35:57 +0000918 return &getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000919 if (Context.isType())
920 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000921 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000922 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000923 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000924 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000925 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000926}
927
Eric Christophera5a79422013-12-09 23:32:48 +0000928DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000929 DIScope Context = resolve(Ty.getContext());
930 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000931
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000932 if (DIE *TyDIE = getDIE(Ty))
David Blaikie409dd9c2013-11-19 23:08:21 +0000933 return TyDIE;
934
935 // Create new type.
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000936 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000937
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000938 constructTypeDIE(TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000939
David Blaikie9d861be2013-11-26 00:15:27 +0000940 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000941 return &TyDIE;
David Blaikie409dd9c2013-11-19 23:08:21 +0000942}
943
Devang Patel0e821f42011-04-12 23:21:44 +0000944/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
945/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000946DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000947 if (!TyNode)
Craig Topper353eda42014-04-24 06:44:33 +0000948 return nullptr;
Manman Renf4c339e2013-10-29 22:49:29 +0000949
David Blaikie32887552013-11-14 22:25:02 +0000950 DIType Ty(TyNode);
951 assert(Ty.isType());
Adrian Prantlc95ec912014-03-24 21:33:01 +0000952 assert(Ty == resolve(Ty.getRef()) &&
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000953 "type was not uniqued, possible ODR violation.");
David Blaikie32887552013-11-14 22:25:02 +0000954
David Blaikieee11f222014-04-12 05:35:59 +0000955 // DW_TAG_restrict_type is not supported in DWARF2
956 if (Ty.getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
957 return getOrCreateTypeDIE(resolve(DIDerivedType(Ty).getTypeDerivedFrom()));
958
Manman Renf4c339e2013-10-29 22:49:29 +0000959 // Construct the context before querying for the existence of the DIE in case
960 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000961 DIScope Context = resolve(Ty.getContext());
962 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000963 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000964
David Blaikie65a74662014-04-25 18:26:14 +0000965 if (DIE *TyDIE = getDIE(Ty))
Devang Patel0e821f42011-04-12 23:21:44 +0000966 return TyDIE;
967
968 // Create new type.
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000969 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
Manman Renf4c339e2013-10-29 22:49:29 +0000970
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000971 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikiec3d9e9e2014-03-06 01:42:00 +0000972
Devang Patel0e821f42011-04-12 23:21:44 +0000973 if (Ty.isBasicType())
David Blaikie65a74662014-04-25 18:26:14 +0000974 constructTypeDIE(TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000975 else if (Ty.isCompositeType()) {
976 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000977 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
978 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +0000979 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
Adrian Prantle2da17f2014-03-14 23:08:17 +0000980 // Skip updating the accelerator tables since this is not the full type.
David Blaikie65a74662014-04-25 18:26:14 +0000981 return &TyDIE;
David Blaikiecfb21152014-01-03 18:59:42 +0000982 }
David Blaikie65a74662014-04-25 18:26:14 +0000983 constructTypeDIE(TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000984 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000985 assert(Ty.isDerivedType() && "Unknown kind of DIType");
David Blaikie65a74662014-04-25 18:26:14 +0000986 constructTypeDIE(TyDIE, DIDerivedType(Ty));
Devang Patel0e821f42011-04-12 23:21:44 +0000987 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000988
David Blaikie65a74662014-04-25 18:26:14 +0000989 return &TyDIE;
David Blaikie2ea848b2013-11-19 22:51:04 +0000990}
991
Eric Christophera5a79422013-12-09 23:32:48 +0000992void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000993 const DIE &TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000994 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
995 bool IsImplementation = 0;
996 if (Ty.isCompositeType()) {
997 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000998 // A runtime language of 0 actually means C/C++ and that any
999 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001000 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001001 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001002 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
David Blaikie18d33752014-04-24 01:23:49 +00001003 DD->addAccelType(Ty.getName(), TyDIE, Flags);
David Blaikie9d861be2013-11-26 00:15:27 +00001004
David Blaikieadbea1e2014-03-12 03:34:38 +00001005 if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1006 Context.isNameSpace()) &&
1007 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001008 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] =
1009 &TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001010 }
Devang Patel0e821f42011-04-12 23:21:44 +00001011}
1012
1013/// addType - Add a new type attribute to the specified entity.
David Blaikie65a74662014-04-25 18:26:14 +00001014void DwarfUnit::addType(DIE &Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001015 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001016
1017 // Check for pre-existence.
1018 DIEEntry *Entry = getDIEEntry(Ty);
1019 // If it exists then use the existing value.
1020 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001021 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001022 return;
1023 }
1024
1025 // Construct type.
1026 DIE *Buffer = getOrCreateTypeDIE(Ty);
1027
1028 // Set up proxy.
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001029 Entry = createDIEEntry(*Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001030 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001031 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001032}
1033
Eric Christopher9c58f312013-09-20 22:20:55 +00001034/// addGlobalName - Add a new global name to the compile unit.
David Blaikie65a74662014-04-25 18:26:14 +00001035void DwarfUnit::addGlobalName(StringRef Name, DIE &Die, DIScope Context) {
David Blaikieadbea1e2014-03-12 03:34:38 +00001036 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1037 return;
Eric Christopher8dba0d52013-10-19 01:04:42 +00001038 std::string FullName = getParentContextString(Context) + Name.str();
David Blaikie65a74662014-04-25 18:26:14 +00001039 GlobalNames[FullName] = &Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001040}
1041
Eric Christopher2c8b7902013-10-17 02:06:06 +00001042/// getParentContextString - Walks the metadata parent chain in a language
1043/// specific manner (using the compile unit language) and returns
1044/// it as a string. This is done at the metadata level because DIEs may
1045/// not currently have been added to the parent context and walking the
1046/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001047std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001048 if (!Context)
1049 return "";
1050
1051 // FIXME: Decide whether to implement this for non-C++ languages.
1052 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1053 return "";
1054
Eric Christopher8dba0d52013-10-19 01:04:42 +00001055 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001056 SmallVector<DIScope, 1> Parents;
1057 while (!Context.isCompileUnit()) {
1058 Parents.push_back(Context);
1059 if (Context.getContext())
1060 Context = resolve(Context.getContext());
1061 else
1062 // Structure, etc types will have a NULL context if they're at the top
1063 // level.
1064 break;
1065 }
1066
1067 // Reverse iterate over our list to go from the outermost construct to the
1068 // innermost.
1069 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1070 E = Parents.rend();
1071 I != E; ++I) {
1072 DIScope Ctx = *I;
1073 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001074 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001075 CS += Name;
1076 CS += "::";
1077 }
1078 }
1079 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001080}
1081
1082/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001083void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001084 // Get core information.
1085 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001086 // Add name if not anonymous or intermediate type.
1087 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001088 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001089
David Blaikiefac56122013-10-04 23:21:16 +00001090 // An unspecified type only has a name attribute.
1091 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001092 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001093
David Blaikie65a74662014-04-25 18:26:14 +00001094 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001095 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001096
Devang Patel0e821f42011-04-12 23:21:44 +00001097 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikie65a74662014-04-25 18:26:14 +00001098 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001099}
1100
1101/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001102void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001103 // Get core information.
1104 StringRef Name = DTy.getName();
1105 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001106 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001107
1108 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001109 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001110 if (FromTy)
David Blaikie65a74662014-04-25 18:26:14 +00001111 addType(Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001112
1113 // Add name if not anonymous or intermediate type.
1114 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001115 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001116
1117 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001118 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikie65a74662014-04-25 18:26:14 +00001119 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001120
David Blaikie5d3249b2013-01-07 05:51:15 +00001121 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
David Blaikie65a74662014-04-25 18:26:14 +00001122 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001123 *getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001124 // Add source line info if available and TyDesc is not a forward declaration.
1125 if (!DTy.isForwardDecl())
David Blaikie65a74662014-04-25 18:26:14 +00001126 addSourceLine(Buffer, DTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001127}
1128
Adrian Prantl3f49c892014-02-25 19:57:42 +00001129/// constructSubprogramArguments - Construct function argument DIEs.
David Blaikie899ae612014-04-30 22:58:19 +00001130void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
Adrian Prantl69140d22014-02-25 22:27:14 +00001131 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1132 DIDescriptor Ty = Args.getElement(i);
1133 if (Ty.isUnspecifiedParameter()) {
1134 assert(i == N-1 && "Unspecified parameter must be the last argument");
1135 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1136 } else {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001137 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1138 addType(Arg, DIType(Ty));
Adrian Prantl69140d22014-02-25 22:27:14 +00001139 if (DIType(Ty).isArtificial())
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001140 addFlag(Arg, dwarf::DW_AT_artificial);
Adrian Prantl3f49c892014-02-25 19:57:42 +00001141 }
Adrian Prantl69140d22014-02-25 22:27:14 +00001142 }
Adrian Prantl3f49c892014-02-25 19:57:42 +00001143}
1144
Devang Patel0e821f42011-04-12 23:21:44 +00001145/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001146void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001147 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001148 StringRef Name = CTy.getName();
1149
1150 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001151 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001152
1153 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001154 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001155 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001156 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001157 case dwarf::DW_TAG_enumeration_type:
1158 constructEnumTypeDIE(Buffer, CTy);
1159 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001160 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001161 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001162 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001163 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001164 if (RTy)
David Blaikie65a74662014-04-25 18:26:14 +00001165 addType(Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001166
1167 bool isPrototyped = true;
Adrian Prantl3f49c892014-02-25 19:57:42 +00001168 if (Elements.getNumElements() == 2 &&
1169 Elements.getElement(1).isUnspecifiedParameter())
1170 isPrototyped = false;
1171
1172 constructSubprogramArguments(Buffer, Elements);
1173
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001174 // Add prototype flag if we're dealing with a C language and the
1175 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001176 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001177 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001178 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001179 Language == dwarf::DW_LANG_ObjC))
David Blaikie65a74662014-04-25 18:26:14 +00001180 addFlag(Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001181
1182 if (CTy.isLValueReference())
David Blaikie65a74662014-04-25 18:26:14 +00001183 addFlag(Buffer, dwarf::DW_AT_reference);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001184
1185 if (CTy.isRValueReference())
David Blaikie65a74662014-04-25 18:26:14 +00001186 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001187 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001188 case dwarf::DW_TAG_structure_type:
1189 case dwarf::DW_TAG_union_type:
1190 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001191 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001192 DIArray Elements = CTy.getTypeArray();
1193 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001194 DIDescriptor Element = Elements.getElement(i);
Adrian Prantlef129fb2014-01-18 02:12:00 +00001195 if (Element.isSubprogram())
David Blaikie65a74662014-04-25 18:26:14 +00001196 getOrCreateSubprogramDIE(DISubprogram(Element));
Adrian Prantlef129fb2014-01-18 02:12:00 +00001197 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001198 DIDerivedType DDTy(Element);
1199 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001200 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001201 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001202 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001203 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001204 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001205 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001206 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001207 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001208 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001209 DIObjCProperty Property(Element);
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001210 DIE &ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001211 StringRef PropertyName = Property.getObjCPropertyName();
1212 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001213 if (Property.getType())
1214 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001215 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001216 StringRef GetterName = Property.getObjCPropertyGetterName();
1217 if (!GetterName.empty())
1218 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1219 StringRef SetterName = Property.getObjCPropertySetterName();
1220 if (!SetterName.empty())
1221 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1222 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001223 if (Property.isReadOnlyObjCProperty())
1224 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1225 if (Property.isReadWriteObjCProperty())
1226 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1227 if (Property.isAssignObjCProperty())
1228 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1229 if (Property.isRetainObjCProperty())
1230 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1231 if (Property.isCopyObjCProperty())
1232 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1233 if (Property.isNonAtomicObjCProperty())
1234 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1235 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001236 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001237 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001238
Devang Pateld925d1a2012-02-07 23:33:58 +00001239 DIEEntry *Entry = getDIEEntry(Element);
1240 if (!Entry) {
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001241 Entry = createDIEEntry(ElemDie);
Devang Pateld925d1a2012-02-07 23:33:58 +00001242 insertDIEEntry(Element, Entry);
1243 }
Devang Patel403e8192012-02-04 01:30:32 +00001244 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001245 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001246 }
1247
1248 if (CTy.isAppleBlockExtension())
David Blaikie65a74662014-04-25 18:26:14 +00001249 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001250
Eric Christopher9e429ae2013-10-05 00:27:02 +00001251 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001252 if (ContainingType)
David Blaikie65a74662014-04-25 18:26:14 +00001253 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001254 *getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001255
Devang Patel12419ae2011-05-12 21:29:42 +00001256 if (CTy.isObjcClassComplete())
David Blaikie65a74662014-04-25 18:26:14 +00001257 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001258
Eric Christopherda011dd2011-12-16 23:42:42 +00001259 // Add template parameters to a class, structure or union types.
1260 // FIXME: The support isn't in the metadata for this yet.
1261 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001262 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001263 addTemplateParams(Buffer, CTy.getTemplateParams());
1264
1265 break;
1266 }
1267 default:
1268 break;
1269 }
1270
1271 // Add name if not anonymous or intermediate type.
1272 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001273 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001274
Eric Christopher775cbd22012-05-22 18:45:18 +00001275 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001276 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001277 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001278 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001279 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001280 if (Size)
David Blaikie65a74662014-04-25 18:26:14 +00001281 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001282 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001283 // Add zero size if it is not a forward declaration.
David Blaikie65a74662014-04-25 18:26:14 +00001284 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001285
1286 // If we're a forward decl, say so.
1287 if (CTy.isForwardDecl())
David Blaikie65a74662014-04-25 18:26:14 +00001288 addFlag(Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001289
1290 // Add source line info if available.
1291 if (!CTy.isForwardDecl())
David Blaikie65a74662014-04-25 18:26:14 +00001292 addSourceLine(Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001293
1294 // No harm in adding the runtime language to the declaration.
1295 unsigned RLang = CTy.getRunTimeLang();
1296 if (RLang)
David Blaikie65a74662014-04-25 18:26:14 +00001297 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001298 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001299 }
1300}
1301
Manman Renffc9a712013-10-23 23:05:28 +00001302/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1303/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001304void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1305 DITemplateTypeParameter TP) {
David Blaikie65a74662014-04-25 18:26:14 +00001306 DIE &ParamDIE =
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001307 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001308 // Add the type if it exists, it could be void and therefore no type.
1309 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001310 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001311 if (!TP.getName().empty())
1312 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001313}
1314
Manman Renffc9a712013-10-23 23:05:28 +00001315/// constructTemplateValueParameterDIE - Construct new DIE for the given
1316/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001317void
1318DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001319 DITemplateValueParameter VP) {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001320 DIE &ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001321
1322 // Add the type if there is one, template template and template parameter
1323 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001324 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001325 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001326 if (!VP.getName().empty())
1327 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1328 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001329 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
David Blaikiec0a28412014-05-11 15:56:59 +00001330 addConstantValue(ParamDIE, CI, resolve(VP.getType()));
David Blaikiea1e813d2013-05-10 21:52:07 +00001331 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1332 // For declaration non-type template parameters (such as global values and
1333 // functions)
Eric Christopher4a741042014-02-16 08:46:55 +00001334 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001335 addOpAddress(*Loc, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001336 // Emit DW_OP_stack_value to use the address as the immediate value of the
1337 // parameter, rather than a pointer to it.
David Blaikie65a74662014-04-25 18:26:14 +00001338 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
Eric Christopher4a741042014-02-16 08:46:55 +00001339 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafb2c412013-08-08 07:40:31 +00001340 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001341 assert(isa<MDString>(Val));
1342 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1343 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001344 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001345 assert(isa<MDNode>(Val));
1346 DIArray A(cast<MDNode>(Val));
David Blaikie65a74662014-04-25 18:26:14 +00001347 addTemplateParams(ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001348 }
1349 }
Devang Patel0e821f42011-04-12 23:21:44 +00001350}
1351
Devang Patel17b53272011-05-06 16:57:54 +00001352/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001353DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001354 // Construct the context before querying for the existence of the DIE in case
1355 // such construction creates the DIE.
1356 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001357
David Blaikie65a74662014-04-25 18:26:14 +00001358 if (DIE *NDie = getDIE(NS))
Devang Patel17b53272011-05-06 16:57:54 +00001359 return NDie;
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001360 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
Manman Renf6b936b2013-10-29 05:49:41 +00001361
Eric Christopher4996c702011-11-07 09:24:32 +00001362 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001363 addString(NDie, dwarf::DW_AT_name, NS.getName());
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001364 DD->addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001365 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001366 } else
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001367 DD->addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001368 addSourceLine(NDie, NS);
David Blaikie65a74662014-04-25 18:26:14 +00001369 return &NDie;
Devang Patel17b53272011-05-06 16:57:54 +00001370}
1371
Devang Patel89543712011-08-15 17:24:54 +00001372/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001373DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001374 // Construct the context before querying for the existence of the DIE in case
1375 // such construction creates the DIE (as is the case for member function
1376 // declarations).
David Blaikie7f916862014-05-27 18:37:38 +00001377 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
Adrian Prantld486b342014-03-18 17:41:15 +00001378
David Blaikie65a74662014-04-25 18:26:14 +00001379 if (DIE *SPDie = getDIE(SP))
Devang Patel89543712011-08-15 17:24:54 +00001380 return SPDie;
1381
David Blaikiece7a1bd2014-05-21 18:04:33 +00001382 if (DISubprogram SPDecl = SP.getFunctionDeclaration()) {
Manman Ren73d697c2013-10-29 00:58:04 +00001383 // Add subprogram definitions to the CU die directly.
David Blaikiebd579052014-04-28 21:14:27 +00001384 ContextDIE = &getUnitDie();
Alp Tokerda0c7932014-05-31 21:26:28 +00001385 // Build the decl now to ensure it precedes the definition.
David Blaikie7f916862014-05-27 18:37:38 +00001386 getOrCreateSubprogramDIE(SPDecl);
David Blaikiece7a1bd2014-05-21 18:04:33 +00001387 }
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001388
1389 // DW_TAG_inlined_subroutine may refer to this DIE.
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001390 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001391
David Blaikie6cfa9e12014-06-05 00:25:26 +00001392 // Stop here and fill this in later, depending on whether or not this
David Blaikief7221ad2014-05-27 18:37:43 +00001393 // subprogram turns out to have inlined instances or not.
1394 if (SP.isDefinition())
1395 return &SPDie;
1396
David Blaikie7f916862014-05-27 18:37:38 +00001397 applySubprogramAttributes(SP, SPDie);
1398 return &SPDie;
1399}
1400
1401void DwarfUnit::applySubprogramAttributes(DISubprogram SP, DIE &SPDie) {
1402 DIE *DeclDie = nullptr;
1403 StringRef DeclLinkageName;
1404 if (DISubprogram SPDecl = SP.getFunctionDeclaration()) {
David Blaikief7221ad2014-05-27 18:37:43 +00001405 DeclDie = getDIE(SPDecl);
David Blaikie6cfa9e12014-06-05 00:25:26 +00001406 assert(DeclDie && "This DIE should've already been constructed when the "
1407 "definition DIE was creaeted in "
1408 "getOrCreateSubprogramDIE");
David Blaikie7f916862014-05-27 18:37:38 +00001409 DeclLinkageName = SPDecl.getLinkageName();
1410 }
Rafael Espindola79278362011-11-10 22:34:29 +00001411
Devang Patel89543712011-08-15 17:24:54 +00001412 // Add function template parameters.
David Blaikie65a74662014-04-25 18:26:14 +00001413 addTemplateParams(SPDie, SP.getTemplateParams());
Devang Patel89543712011-08-15 17:24:54 +00001414
Adrian Prantl8714aaf2014-04-14 21:16:04 +00001415 // Add the linkage name if we have one and it isn't in the Decl.
1416 StringRef LinkageName = SP.getLinkageName();
David Blaikiece7a1bd2014-05-21 18:04:33 +00001417 assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1418 LinkageName == DeclLinkageName) &&
1419 "decl has a linkage name and it is different");
1420 if (!LinkageName.empty() && DeclLinkageName.empty())
1421 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1422 GlobalValue::getRealLinkageName(LinkageName));
Devang Patel89543712011-08-15 17:24:54 +00001423
David Blaikiece7a1bd2014-05-21 18:04:33 +00001424 if (DeclDie) {
1425 // Refer to the function declaration where all the other attributes will be
1426 // found.
1427 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
David Blaikie7f916862014-05-27 18:37:38 +00001428 return;
David Blaikiece7a1bd2014-05-21 18:04:33 +00001429 }
Eric Christopheracb71152012-08-23 22:52:55 +00001430
Devang Patel89543712011-08-15 17:24:54 +00001431 // Constructors and operators for anonymous aggregates do not have names.
1432 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001433 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001434
1435 addSourceLine(SPDie, SP);
1436
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001437 // Add the prototype if we have a prototype and we have a C like
1438 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001439 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001440 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001441 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001442 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001443 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001444
Devang Patel89543712011-08-15 17:24:54 +00001445 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001446 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1447 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001448
David Blaikie5174c842013-05-22 23:22:18 +00001449 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001450 // Add a return type. If this is a type like a C/C++ void type we don't add a
1451 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001452 if (Args.getElement(0))
1453 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001454
1455 unsigned VK = SP.getVirtuality();
1456 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001457 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Eric Christopher4a741042014-02-16 08:46:55 +00001458 DIELoc *Block = getDIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001459 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1460 addUInt(*Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
David Blaikief2443192013-10-21 17:28:37 +00001461 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001462 ContainingTypeMap.insert(
David Blaikie65a74662014-04-25 18:26:14 +00001463 std::make_pair(&SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001464 }
1465
1466 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001467 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001468
Devang Patel89543712011-08-15 17:24:54 +00001469 // Add arguments. Do not add arguments for subprogram definition. They will
1470 // be handled while processing variables.
David Blaikie899ae612014-04-30 22:58:19 +00001471 constructSubprogramArguments(SPDie, Args);
Devang Patel89543712011-08-15 17:24:54 +00001472 }
1473
1474 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001475 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001476
1477 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001478 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001479
1480 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001481 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001482
1483 if (unsigned isa = Asm->getISAEncoding()) {
1484 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1485 }
1486
Adrian Prantl99c7af22013-12-18 21:48:19 +00001487 if (SP.isLValueReference())
1488 addFlag(SPDie, dwarf::DW_AT_reference);
1489
1490 if (SP.isRValueReference())
1491 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1492
Adrian Prantlef129fb2014-01-18 02:12:00 +00001493 if (SP.isProtected())
1494 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1495 dwarf::DW_ACCESS_protected);
1496 else if (SP.isPrivate())
1497 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1498 dwarf::DW_ACCESS_private);
1499 else
1500 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1501 dwarf::DW_ACCESS_public);
1502
1503 if (SP.isExplicit())
1504 addFlag(SPDie, dwarf::DW_AT_explicit);
Devang Patel89543712011-08-15 17:24:54 +00001505}
1506
Devang Pateldfd6ec32011-08-15 17:57:41 +00001507// Return const expression if value is a GEP to access merged global
1508// constant. e.g.
1509// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1510static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1511 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1512 if (!CE || CE->getNumOperands() != 3 ||
1513 CE->getOpcode() != Instruction::GetElementPtr)
Craig Topper353eda42014-04-24 06:44:33 +00001514 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001515
1516 // First operand points to a global struct.
1517 Value *Ptr = CE->getOperand(0);
1518 if (!isa<GlobalValue>(Ptr) ||
1519 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
Craig Topper353eda42014-04-24 06:44:33 +00001520 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001521
1522 // Second operand is zero.
1523 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1524 if (!CI || !CI->isZero())
Craig Topper353eda42014-04-24 06:44:33 +00001525 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001526
1527 // Third operand is offset.
1528 if (!isa<ConstantInt>(CE->getOperand(2)))
Craig Topper353eda42014-04-24 06:44:33 +00001529 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001530
1531 return CE;
1532}
1533
1534/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001535void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001536 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001537 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001538 return;
1539
David Blaikie5e390e42014-02-04 01:23:52 +00001540 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001541
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001542 DIScope GVContext = GV.getContext();
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001543 DIType GTy = DD->resolve(GV.getType());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001544
1545 // If this is a static data member definition, some attributes belong
1546 // to the declaration DIE.
Craig Topper353eda42014-04-24 06:44:33 +00001547 DIE *VariableDIE = nullptr;
Manman Rene697d3c2013-02-01 23:54:37 +00001548 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001549 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1550 if (SDMDecl.Verify()) {
1551 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1552 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001553 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001554 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001555 }
1556
1557 // If this is not a static data member definition, create the variable
1558 // DIE and add the initial set of attributes to it.
1559 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001560 // Construct the context before querying for the existence of the DIE in
1561 // case such construction creates the DIE.
1562 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001563
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001564 // Add to map.
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001565 VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001566
1567 // Add name and type.
David Blaikie65a74662014-04-25 18:26:14 +00001568 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1569 addType(*VariableDIE, GTy);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001570
1571 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001572 if (!GV.isLocalToUnit())
David Blaikie65a74662014-04-25 18:26:14 +00001573 addFlag(*VariableDIE, dwarf::DW_AT_external);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001574
1575 // Add line number info.
David Blaikie65a74662014-04-25 18:26:14 +00001576 addSourceLine(*VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001577 }
1578
Devang Pateldfd6ec32011-08-15 17:57:41 +00001579 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001580 bool addToAccelTable = false;
Craig Topper353eda42014-04-24 06:44:33 +00001581 DIE *VariableSpecDIE = nullptr;
1582 bool isGlobalVariable = GV.getGlobal() != nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001583 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001584 addToAccelTable = true;
Eric Christopher4a741042014-02-16 08:46:55 +00001585 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001586 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001587 if (GV.getGlobal()->isThreadLocal()) {
1588 // FIXME: Make this work with -gsplit-dwarf.
1589 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1590 assert((PointerSize == 4 || PointerSize == 8) &&
1591 "Add support for other sizes if necessary");
1592 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001593 if (!DD->useSplitDwarf()) {
1594 // 1) Start with a constNu of the appropriate pointer size
David Blaikie65a74662014-04-25 18:26:14 +00001595 addUInt(*Loc, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001596 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001597 // 2) containing the (relocated) offset of the TLS variable
1598 // within the module's TLS block.
David Blaikie65a74662014-04-25 18:26:14 +00001599 addExpr(*Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001600 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001601 } else {
David Blaikie65a74662014-04-25 18:26:14 +00001602 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1603 addUInt(*Loc, dwarf::DW_FORM_udata,
David Blaikied75fb282014-04-23 21:20:10 +00001604 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001605 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001606 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikie65a74662014-04-25 18:26:14 +00001607 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001608 } else {
1609 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikie65a74662014-04-25 18:26:14 +00001610 addOpAddress(*Loc, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001611 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001612 // Do not create specification DIE if context is either compile unit
1613 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001614 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001615 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001616 // Create specification DIE.
David Blaikiebd579052014-04-28 21:14:27 +00001617 VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie);
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001618 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE);
David Blaikie65a74662014-04-25 18:26:14 +00001619 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001620 // A static member's declaration is already flagged as such.
1621 if (!SDMDecl.Verify())
David Blaikie65a74662014-04-25 18:26:14 +00001622 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001623 } else {
David Blaikie65a74662014-04-25 18:26:14 +00001624 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4996c702011-11-07 09:24:32 +00001625 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001626 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001627 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001628 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001629 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1630 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1631 // TAG_variable.
David Blaikie65a74662014-04-25 18:26:14 +00001632 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE
1633 : *VariableDIE,
Eric Christopheraf7eca22014-03-13 23:26:25 +00001634 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1635 : dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001636 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001637 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001638 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001639 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001640 // emitting AT_const_value multiple times, we only add AT_const_value when
1641 // it is not a static member.
1642 if (!IsStaticMember)
David Blaikiec0a28412014-05-11 15:56:59 +00001643 addConstantValue(*VariableDIE, CI, GTy);
David Blaikiea781b25b2013-11-17 21:55:13 +00001644 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001645 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001646 // GV is a merged global.
Eric Christopher4a741042014-02-16 08:46:55 +00001647 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Pateldfd6ec32011-08-15 17:57:41 +00001648 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001649 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1650 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikie65a74662014-04-25 18:26:14 +00001651 addOpAddress(*Loc, Sym);
1652 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001653 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikie65a74662014-04-25 18:26:14 +00001654 addUInt(*Loc, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001655 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikie65a74662014-04-25 18:26:14 +00001656 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1657 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001658 }
1659
Eric Christopherc12c2112011-11-11 01:55:22 +00001660 if (addToAccelTable) {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001661 DIE &AddrDIE = VariableSpecDIE ? *VariableSpecDIE : *VariableDIE;
David Blaikie2406a0622014-04-23 23:37:35 +00001662 DD->addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001663
Eric Christopherc12c2112011-11-11 01:55:22 +00001664 // If the linkage name is different than the name, go ahead and output
1665 // that as well into the name table.
1666 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
David Blaikie2406a0622014-04-23 23:37:35 +00001667 DD->addAccelName(GV.getLinkageName(), AddrDIE);
Eric Christopherc12c2112011-11-11 01:55:22 +00001668 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001669
1670 if (!GV.isLocalToUnit())
David Blaikie65a74662014-04-25 18:26:14 +00001671 addGlobalName(GV.getName(),
1672 VariableSpecDIE ? *VariableSpecDIE : *VariableDIE,
Eric Christopher2c8b7902013-10-17 02:06:06 +00001673 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001674}
1675
Devang Patel0e821f42011-04-12 23:21:44 +00001676/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001677void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001678 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001679 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001680
Bill Wendling28fe9e72012-12-06 07:38:10 +00001681 // The LowerBound value defines the lower bounds which is typically zero for
1682 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1683 // Count == -1 then the array is unbounded and we do not emit
1684 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1685 // Count == 0, then the array has zero elements in which case we do not emit
1686 // an upper bound.
1687 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001688 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001689 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001690
Bill Wendling3495f9b2012-12-06 07:55:19 +00001691 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001692 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001693
1694 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001695 // FIXME: An unbounded array should reference the expression that defines
1696 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001697 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1698 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001699}
1700
1701/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001702void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001703 if (CTy.isVector())
David Blaikie65a74662014-04-25 18:26:14 +00001704 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001705
Eric Christopher0df08e22013-08-08 07:40:37 +00001706 // Emit the element type.
David Blaikie65a74662014-04-25 18:26:14 +00001707 addType(Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001708
1709 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001710 // FIXME: This type should be passed down from the front end
1711 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001712 DIE *IdxTy = getIndexTyDie();
1713 if (!IdxTy) {
David Blaikie12e00fc2014-04-03 06:28:20 +00001714 // Construct an integer type to use for indexes.
David Blaikiebd579052014-04-28 21:14:27 +00001715 IdxTy = &createAndAddDIE(dwarf::DW_TAG_base_type, UnitDie);
David Blaikie65a74662014-04-25 18:26:14 +00001716 addString(*IdxTy, dwarf::DW_AT_name, "sizetype");
1717 addUInt(*IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1718 addUInt(*IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
David Blaikie12e00fc2014-04-03 06:28:20 +00001719 dwarf::DW_ATE_unsigned);
Devang Patel0e821f42011-04-12 23:21:44 +00001720 setIndexTyDie(IdxTy);
1721 }
1722
1723 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001724 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIDescriptor Element = Elements.getElement(i);
1727 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1728 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1729 }
1730}
1731
Eric Christopheraeb105f2013-11-11 18:52:39 +00001732/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001733void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001734 DIArray Elements = CTy.getTypeArray();
1735
1736 // Add enumerators to enumeration type.
1737 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001738 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001739 if (Enum.isEnumerator()) {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001740 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001741 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001742 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001743 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001744 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1745 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001746 }
1747 }
1748 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1749 if (DTy) {
David Blaikie65a74662014-04-25 18:26:14 +00001750 addType(Buffer, DTy);
1751 addFlag(Buffer, dwarf::DW_AT_enum_class);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001752 }
Devang Patel0e821f42011-04-12 23:21:44 +00001753}
1754
Devang Patel89543712011-08-15 17:24:54 +00001755/// constructContainingTypeDIEs - Construct DIEs for types that contain
1756/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001757void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001758 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001759 CE = ContainingTypeMap.end();
1760 CI != CE; ++CI) {
David Blaikie65a74662014-04-25 18:26:14 +00001761 DIE &SPDie = *CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001762 DIDescriptor D(CI->second);
1763 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001764 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001765 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001766 if (!NDie)
1767 continue;
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001768 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
Devang Patel89543712011-08-15 17:24:54 +00001769 }
1770}
1771
Devang Patel3acc70e2011-08-15 22:04:40 +00001772/// constructVariableDIE - Construct a DIE for the given DbgVariable.
David Blaikie914046e2014-04-25 20:00:34 +00001773std::unique_ptr<DIE> DwarfUnit::constructVariableDIE(DbgVariable &DV,
David Blaikieb85f0082014-05-27 19:34:32 +00001774 bool Abstract) {
1775 auto D = constructVariableDIEImpl(DV, Abstract);
David Blaikiee071fc82014-04-25 17:32:19 +00001776 DV.setDIE(*D);
1777 return D;
1778}
1779
David Blaikieb85f0082014-05-27 19:34:32 +00001780std::unique_ptr<DIE> DwarfUnit::constructVariableDIEImpl(const DbgVariable &DV,
1781 bool Abstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001782 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001783
Devang Patel3acc70e2011-08-15 22:04:40 +00001784 // Define variable debug information entry.
David Blaikie914046e2014-04-25 20:00:34 +00001785 auto VariableDie = make_unique<DIE>(DV.getTag());
David Blaikie19a8b902014-06-04 01:30:59 +00001786 DbgVariable *AbsVar = DV.getAbstractVariable();
David Blaikie72c3aa32014-06-05 02:04:59 +00001787 if (AbsVar && AbsVar->getDIE()) {
David Blaikieb85f0082014-05-27 19:34:32 +00001788 addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin, *AbsVar->getDIE());
David Blaikie36408e72014-06-04 23:50:52 +00001789 } else {
David Blaikie715528b2013-08-19 03:34:03 +00001790 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001791 addString(*VariableDie, dwarf::DW_AT_name, Name);
1792 addSourceLine(*VariableDie, DV.getVariable());
1793 addType(*VariableDie, DV.getType());
David Blaikieb85f0082014-05-27 19:34:32 +00001794 if (DV.isArtificial())
1795 addFlag(*VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001796 }
1797
David Blaikieb85f0082014-05-27 19:34:32 +00001798 if (Abstract)
Devang Patel3acc70e2011-08-15 22:04:40 +00001799 return VariableDie;
Devang Patel3acc70e2011-08-15 22:04:40 +00001800
1801 // Add variable address.
1802
Eric Christopher34a2c872013-11-15 01:43:19 +00001803 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001804 if (Offset != ~0U) {
David Blaikie65a74662014-04-25 18:26:14 +00001805 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
Devang Patel3acc70e2011-08-15 22:04:40 +00001806 return VariableDie;
1807 }
1808
Eric Christophercead0332011-10-03 15:49:20 +00001809 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001810 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001811 assert(DVInsn->getNumOperands() == 3);
1812 if (DVInsn->getOperand(0).isReg()) {
1813 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001814 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001815 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001816 MachineLocation Location(RegOp.getReg(),
1817 DVInsn->getOperand(1).getImm());
David Blaikie65a74662014-04-25 18:26:14 +00001818 addVariableAddress(DV, *VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001819 } else if (RegOp.getReg())
David Blaikie65a74662014-04-25 18:26:14 +00001820 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001821 } else if (DVInsn->getOperand(0).isImm())
David Blaikie65a74662014-04-25 18:26:14 +00001822 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001823 else if (DVInsn->getOperand(0).isFPImm())
David Blaikie65a74662014-04-25 18:26:14 +00001824 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001825 else if (DVInsn->getOperand(0).isCImm())
David Blaikie65a74662014-04-25 18:26:14 +00001826 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
David Blaikiec0a28412014-05-11 15:56:59 +00001827 DV.getType());
Eric Christopher78fcf4902013-07-03 01:08:30 +00001828
Devang Patel3acc70e2011-08-15 22:04:40 +00001829 return VariableDie;
Devang Patel3acc70e2011-08-15 22:04:40 +00001830 }
1831
David Blaikiee071fc82014-04-25 17:32:19 +00001832 // .. else use frame index.
1833 int FI = DV.getFrameIndex();
1834 if (FI != ~0) {
1835 unsigned FrameReg = 0;
1836 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1837 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1838 MachineLocation Location(FrameReg, Offset);
David Blaikie65a74662014-04-25 18:26:14 +00001839 addVariableAddress(DV, *VariableDie, Location);
David Blaikiee071fc82014-04-25 17:32:19 +00001840 }
1841
Devang Patel3acc70e2011-08-15 22:04:40 +00001842 return VariableDie;
1843}
1844
Manman Ren230ec862013-10-23 23:00:44 +00001845/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001846void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001847 DIE &MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001848 StringRef Name = DT.getName();
1849 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001850 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001851
Manman Ren93b30902013-10-08 18:42:58 +00001852 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001853
1854 addSourceLine(MemberDie, DT);
1855
Eric Christopherc2697f82013-10-19 01:04:47 +00001856 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001857
1858 // For C++, virtual base classes are not at fixed offset. Use following
1859 // expression to extract appropriate offset from vtable.
1860 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1861
Eric Christopher4a741042014-02-16 08:46:55 +00001862 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001863 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1864 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1865 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1866 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1867 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1868 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1869 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001870
David Blaikief2443192013-10-21 17:28:37 +00001871 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001872 } else {
1873 uint64_t Size = DT.getSizeInBits();
1874 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1875 uint64_t OffsetInBytes;
1876
1877 if (Size != FieldSize) {
Eric Christopher1acdbb82014-03-12 17:14:46 +00001878 // Handle bitfield, assume bytes are 8 bits.
1879 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1880 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
David Blaikie71d34a22013-11-01 00:25:45 +00001881
1882 uint64_t Offset = DT.getOffsetInBits();
1883 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1884 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1885 uint64_t FieldOffset = (HiMark - FieldSize);
1886 Offset -= FieldOffset;
1887
1888 // Maybe we need to work from the other end.
1889 if (Asm->getDataLayout().isLittleEndian())
1890 Offset = FieldSize - (Offset + Size);
1891 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1892
Adrian Prantlc67655a2014-01-28 18:13:47 +00001893 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001894 // field that includes this bit field.
1895 OffsetInBytes = FieldOffset >> 3;
1896 } else
1897 // This is not a bitfield.
1898 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001899
David Blaikie22b29a52014-01-03 01:30:05 +00001900 if (DD->getDwarfVersion() <= 2) {
Eric Christopher4a741042014-02-16 08:46:55 +00001901 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001902 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1903 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
David Blaikie22b29a52014-01-03 01:30:05 +00001904 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1905 } else
1906 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1907 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001908 }
Devang Patel0e821f42011-04-12 23:21:44 +00001909
1910 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001911 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001912 dwarf::DW_ACCESS_protected);
1913 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001914 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001915 dwarf::DW_ACCESS_private);
1916 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001917 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001918 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001919 dwarf::DW_ACCESS_public);
1920 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001921 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001922 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001923
1924 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001925 if (MDNode *PNode = DT.getObjCProperty())
1926 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
David Blaikie65a74662014-04-25 18:26:14 +00001927 MemberDie.addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1928 PropertyDie);
Devang Patel44882172012-02-06 17:49:43 +00001929
David Blaikie37fefc32012-12-13 22:43:07 +00001930 if (DT.isArtificial())
1931 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001932}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001933
Manman Renc6b63922013-10-14 20:33:57 +00001934/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001935DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001936 if (!DT.Verify())
Craig Topper353eda42014-04-24 06:44:33 +00001937 return nullptr;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001938
Manman Renc6b63922013-10-14 20:33:57 +00001939 // Construct the context before querying for the existence of the DIE in case
1940 // such construction creates the DIE.
1941 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001942 assert(dwarf::isType(ContextDIE->getTag()) &&
1943 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001944
David Blaikie65a74662014-04-25 18:26:14 +00001945 if (DIE *StaticMemberDIE = getDIE(DT))
Manman Renc6b63922013-10-14 20:33:57 +00001946 return StaticMemberDIE;
1947
David Blaikieb0b3fcf2014-04-25 18:52:29 +00001948 DIE &StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001949
Manman Ren93b30902013-10-08 18:42:58 +00001950 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001951
1952 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1953 addType(StaticMemberDIE, Ty);
1954 addSourceLine(StaticMemberDIE, DT);
1955 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1956 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1957
1958 // FIXME: We could omit private if the parent is a class_type, and
1959 // public if the parent is something else.
1960 if (DT.isProtected())
1961 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1962 dwarf::DW_ACCESS_protected);
1963 else if (DT.isPrivate())
1964 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1965 dwarf::DW_ACCESS_private);
1966 else
1967 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1968 dwarf::DW_ACCESS_public);
1969
1970 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
David Blaikiec0a28412014-05-11 15:56:59 +00001971 addConstantValue(StaticMemberDIE, CI, Ty);
David Blaikiea39a76e2013-01-20 01:18:01 +00001972 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1973 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001974
David Blaikie65a74662014-04-25 18:26:14 +00001975 return &StaticMemberDIE;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001976}
David Blaikie6b288cf2013-10-30 20:42:41 +00001977
David Blaikied82b2372014-03-24 20:28:10 +00001978void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001979 Asm->OutStreamer.AddComment("DWARF version number");
1980 Asm->EmitInt16(DD->getDwarfVersion());
1981 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001982 // We share one abbreviations table across all units so it's always at the
1983 // start of the section. Use a relocatable offset where needed to ensure
1984 // linking doesn't invalidate that offset.
David Blaikie3c9a3cc2014-03-24 20:53:02 +00001985 if (ASectionSym)
1986 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
1987 else
David Blaikie326e1fa2014-04-02 02:04:51 +00001988 // Use a constant value when no symbol is provided.
David Blaikie3c9a3cc2014-03-24 20:53:02 +00001989 Asm->EmitInt32(0);
David Blaikie6b288cf2013-10-30 20:42:41 +00001990 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1991 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1992}
David Blaikiebc563272013-12-13 21:33:40 +00001993
Eric Christopher384f3fe2014-03-20 19:16:16 +00001994void DwarfUnit::addRange(RangeSpan Range) {
1995 // Only add a range for this unit if we're emitting full debug.
1996 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) {
1997 // If we have no current ranges just add the range and return, otherwise,
1998 // check the current section and CU against the previous section and CU we
1999 // emitted into and the subprogram was contained within. If these are the
2000 // same then extend our current range, otherwise add this as a new range.
2001 if (CURanges.size() == 0 ||
2002 this != DD->getPrevCU() ||
2003 Asm->getCurrentSection() != DD->getPrevSection()) {
2004 CURanges.push_back(Range);
2005 return;
2006 }
2007
2008 assert(&(CURanges.back().getEnd()->getSection()) ==
2009 &(Range.getEnd()->getSection()) &&
2010 "We can only append to a range in the same section!");
2011 CURanges.back().setEnd(Range.getEnd());
2012 }
2013}
2014
David Blaikie2494fdb2014-02-14 22:41:51 +00002015void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2016 // Define start line table label for each Compile Unit.
2017 MCSymbol *LineTableStartSym =
David Blaikie34641612014-04-01 08:07:52 +00002018 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
David Blaikie2494fdb2014-02-14 22:41:51 +00002019
David Blaikiebd579052014-04-28 21:14:27 +00002020 stmtListIndex = UnitDie.getValues().size();
David Blaikie60e63862014-02-14 23:58:13 +00002021
David Blaikie2494fdb2014-02-14 22:41:51 +00002022 // DW_AT_stmt_list is a offset of line number information for this
2023 // compile unit in debug_line section. For split dwarf this is
2024 // left in the skeleton CU and so not included.
2025 // The line table entries are not always emitted in assembly, so it
2026 // is not okay to use line_table_start here.
2027 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
David Blaikiebd579052014-04-28 21:14:27 +00002028 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym);
David Blaikie2494fdb2014-02-14 22:41:51 +00002029 else
David Blaikiebd579052014-04-28 21:14:27 +00002030 addSectionDelta(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
David Blaikie2494fdb2014-02-14 22:41:51 +00002031 DwarfLineSectionSym);
2032}
2033
David Blaikie60e63862014-02-14 23:58:13 +00002034void DwarfCompileUnit::applyStmtList(DIE &D) {
2035 D.addValue(dwarf::DW_AT_stmt_list,
David Blaikiebd579052014-04-28 21:14:27 +00002036 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
2037 UnitDie.getValues()[stmtListIndex]);
David Blaikie60e63862014-02-14 23:58:13 +00002038}
2039
David Blaikied82b2372014-03-24 20:28:10 +00002040void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const {
2041 DwarfUnit::emitHeader(ASectionSym);
David Blaikiebc563272013-12-13 21:33:40 +00002042 Asm->OutStreamer.AddComment("Type Signature");
2043 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2044 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002045 // In a skeleton type unit there is no type DIE so emit a zero offset.
2046 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2047 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002048}
2049
2050void DwarfTypeUnit::initSection(const MCSection *Section) {
2051 assert(!this->Section);
2052 this->Section = Section;
2053 // Since each type unit is contained in its own COMDAT section, the begin
2054 // label and the section label are the same. Using the begin label emission in
2055 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2056 // the only other alternative of lazily constructing start-of-section labels
2057 // and storing a mapping in DwarfDebug (or AsmPrinter).
2058 this->SectionSym = this->LabelBegin =
2059 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2060 this->LabelEnd =
2061 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
David Blaikiebc563272013-12-13 21:33:40 +00002062}