blob: a70c0f7c11ae428a82796864e8cdf0415d6ce218 [file] [log] [blame]
Stephen Hines36b56882014-04-23 16:57:46 -07001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel161b2f42011-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 Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
Stephen Hines36b56882014-04-23 16:57:46 -070014#include "DwarfUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000016#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/APFloat.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Stephen Hines36b56882014-04-23 16:57:46 -070019#include "llvm/IR/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/GlobalVariable.h"
22#include "llvm/IR/Instructions.h"
Stephen Hines36b56882014-04-23 16:57:46 -070023#include "llvm/IR/Mangler.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
David Blaikie1d361132013-10-30 20:42:41 +000026#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCStreamer.h"
Stephen Hines36b56882014-04-23 16:57:46 -070028#include "llvm/Support/CommandLine.h"
Devang Patel161b2f42011-04-12 23:21:44 +000029#include "llvm/Target/TargetFrameLowering.h"
David Blaikie59eaa382013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Stephen Hines36b56882014-04-23 16:57:46 -070031#include "llvm/Target/TargetMachine.h"
Devang Patel161b2f42011-04-12 23:21:44 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000033
34using namespace llvm;
35
Stephen Hinesdce4a402014-05-29 02:49:00 -070036#define DEBUG_TYPE "dwarfdebug"
37
Stephen Hines36b56882014-04-23 16:57:46 -070038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
42
43/// Unit - Unit constructor.
Stephen Hinesdce4a402014-05-29 02:49:00 -070044DwarfUnit::DwarfUnit(unsigned UID, dwarf::Tag UnitTag, DICompileUnit Node,
45 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
46 : UniqueID(UID), CUNode(Node), UnitDie(UnitTag), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr),
48 Skeleton(nullptr) {
49 assert(UnitTag == dwarf::DW_TAG_compile_unit ||
50 UnitTag == dwarf::DW_TAG_type_unit);
Devang Patel161b2f42011-04-12 23:21:44 +000051 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Stephen Hines36b56882014-04-23 16:57:46 -070052}
53
Stephen Hinesdce4a402014-05-29 02:49:00 -070054DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node,
Stephen Hines36b56882014-04-23 16:57:46 -070055 AsmPrinter *A, DwarfDebug *DW,
56 DwarfFile *DWU)
Stephen Hinesdce4a402014-05-29 02:49:00 -070057 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) {
58 insertDIE(Node, &getUnitDie());
Devang Patel161b2f42011-04-12 23:21:44 +000059}
60
Stephen Hinesdce4a402014-05-29 02:49:00 -070061DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A,
62 DwarfDebug *DW, DwarfFile *DWU,
Stephen Hines36b56882014-04-23 16:57:46 -070063 MCDwarfDwoLineTable *SplitLineTable)
Stephen Hinesdce4a402014-05-29 02:49:00 -070064 : DwarfUnit(UID, dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU),
65 CU(CU), SplitLineTable(SplitLineTable) {
Stephen Hines36b56882014-04-23 16:57:46 -070066 if (SplitLineTable)
Stephen Hinesdce4a402014-05-29 02:49:00 -070067 addSectionOffset(UnitDie, dwarf::DW_AT_stmt_list, 0);
Stephen Hines36b56882014-04-23 16:57:46 -070068}
69
70/// ~Unit - Destructor for compile unit.
71DwarfUnit::~DwarfUnit() {
Devang Patel161b2f42011-04-12 23:21:44 +000072 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
73 DIEBlocks[j]->~DIEBlock();
Stephen Hines36b56882014-04-23 16:57:46 -070074 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
75 DIELocs[j]->~DIELoc();
Devang Patel161b2f42011-04-12 23:21:44 +000076}
77
78/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
79/// information entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -070080DIEEntry *DwarfUnit::createDIEEntry(DIE &Entry) {
Devang Patel161b2f42011-04-12 23:21:44 +000081 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
82 return Value;
83}
84
Bill Wendling6afe4782012-12-06 07:55:19 +000085/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000086/// DWARF version doesn't handle the language, return -1.
Stephen Hines36b56882014-04-23 16:57:46 -070087int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikieaedaa722013-11-15 23:50:53 +000088 switch (getLanguage()) {
Bill Wendling222c2fd2012-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 Renb8b70e12013-10-31 17:54:35 +0000129/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie86a33482013-11-15 22:59:36 +0000130static bool isShareableAcrossCUs(DIDescriptor D) {
Stephen Hines36b56882014-04-23 16:57:46 -0700131 // 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.
138 return (D.isType() ||
139 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
140 !GenerateDwarfTypeUnits;
Manman Renb8b70e12013-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.
Stephen Hines36b56882014-04-23 16:57:46 -0700147DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikiecbc85a22013-11-15 23:09:13 +0000148 if (isShareableAcrossCUs(D))
149 return DD->getDIE(D);
150 return MDNodeToDieMap.lookup(D);
Manman Renb8b70e12013-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.
Stephen Hines36b56882014-04-23 16:57:46 -0700156void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikiecbc85a22013-11-15 23:09:13 +0000157 if (isShareableAcrossCUs(Desc)) {
158 DD->insertDIE(Desc, D);
Manman Renb8b70e12013-10-31 17:54:35 +0000159 return;
160 }
David Blaikiecbc85a22013-11-15 23:09:13 +0000161 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Renb8b70e12013-10-31 17:54:35 +0000162}
163
Eric Christopher873cf0a2012-08-24 01:14:27 +0000164/// addFlag - Add a flag that is true.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700165void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
Michael Gottesmandc42d032013-09-04 04:39:38 +0000166 if (DD->getDwarfVersion() >= 4)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700167 Die.addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopher873cf0a2012-08-24 01:14:27 +0000168 else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700169 Die.addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopher873cf0a2012-08-24 01:14:27 +0000170}
171
Devang Patel161b2f42011-04-12 23:21:44 +0000172/// addUInt - Add an unsigned integer attribute data and value.
173///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700174void DwarfUnit::addUInt(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700175 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopher6efc0432013-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);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700180 Die.addValue(Attribute, *Form, Value);
David Blaikie770530b2013-10-21 17:28:37 +0000181}
182
Stephen Hinesdce4a402014-05-29 02:49:00 -0700183void DwarfUnit::addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer) {
David Blaikie770530b2013-10-21 17:28:37 +0000184 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel161b2f42011-04-12 23:21:44 +0000185}
186
187/// addSInt - Add an signed integer attribute data and value.
188///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700189void DwarfUnit::addSInt(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700190 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopher6efc0432013-10-19 01:04:47 +0000191 if (!Form)
192 Form = DIEInteger::BestForm(true, Integer);
Devang Patel161b2f42011-04-12 23:21:44 +0000193 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700194 Die.addValue(Attribute, *Form, Value);
David Blaikie770530b2013-10-21 17:28:37 +0000195}
196
Stephen Hinesdce4a402014-05-29 02:49:00 -0700197void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
Stephen Hines36b56882014-04-23 16:57:46 -0700198 int64_t Integer) {
David Blaikie770530b2013-10-21 17:28:37 +0000199 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel161b2f42011-04-12 23:21:44 +0000200}
201
Nick Lewycky6a7efcf2011-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 Christopher3cc42202013-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700207void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700208 StringRef String) {
209
210 if (!DD->useSplitDwarf())
211 return addLocalString(Die, Attribute, String);
212
Stephen Hinesdce4a402014-05-29 02:49:00 -0700213 unsigned idx = DU->getStringPool().getIndex(*Asm, String);
Stephen Hines36b56882014-04-23 16:57:46 -0700214 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher3dee5752013-07-26 17:02:41 +0000215 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700216 Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopherdd8e9f32013-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700221void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700222 StringRef String) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700223 MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000224 DIEValue *Value;
Stephen Hines36b56882014-04-23 16:57:46 -0700225 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000226 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000227 else {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700228 MCSymbol *StringPool = DU->getStringPool().getSectionSymbol();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000229 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000230 }
Stephen Hines36b56882014-04-23 16:57:46 -0700231 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700232 Die.addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel161b2f42011-04-12 23:21:44 +0000233}
234
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000235/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel161b2f42011-04-12 23:21:44 +0000236///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700237void DwarfUnit::addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000238 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700239 Die.addValue((dwarf::Attribute)0, Form, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000240}
241
Stephen Hines36b56882014-04-23 16:57:46 -0700242/// addLocationList - Add a Dwarf loclistptr attribute data and value.
243///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700244void DwarfUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700245 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;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700249 Die.addValue(Attribute, Form, Value);
Stephen Hines36b56882014-04-23 16:57:46 -0700250}
251
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000252/// addLabel - Add a Dwarf label attribute data and value.
253///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700254void DwarfUnit::addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
Stephen Hines36b56882014-04-23 16:57:46 -0700255 const MCSymbol *Label) {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000256 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700257 Die.addValue(Attribute, Form, Value);
David Blaikie95e72c92013-06-28 20:05:04 +0000258}
259
Stephen Hinesdce4a402014-05-29 02:49:00 -0700260void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikie770530b2013-10-21 17:28:37 +0000261 addLabel(Die, (dwarf::Attribute)0, Form, Label);
262}
263
Stephen Hines36b56882014-04-23 16:57:46 -0700264/// addSectionLabel - Add a Dwarf section label attribute data and value.
265///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700266void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700267 const MCSymbol *Label) {
268 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///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700276void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700277 uint64_t Integer) {
278 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 Christopher72f7bfb2013-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///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700287void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700288 const MCSymbol *Label) {
289
290 if (!DD->useSplitDwarf())
291 return addLocalLabelAddress(Die, Attribute, Label);
292
Alexey Samsonov9d08d692013-10-03 08:54:43 +0000293 if (Label)
294 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton5cc319a2013-09-19 23:21:01 +0000295
Stephen Hinesdce4a402014-05-29 02:49:00 -0700296 unsigned idx = DD->getAddressPool().getIndex(Label);
Stephen Hines36b56882014-04-23 16:57:46 -0700297 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700298 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Stephen Hines36b56882014-04-23 16:57:46 -0700299}
300
Stephen Hinesdce4a402014-05-29 02:49:00 -0700301void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
Stephen Hines36b56882014-04-23 16:57:46 -0700302 dwarf::Attribute Attribute,
303 const MCSymbol *Label) {
304 if (Label)
305 DD->addArangeLabel(SymbolCU(this, Label));
306
Stephen Hinesdce4a402014-05-29 02:49:00 -0700307 Die.addValue(Attribute, dwarf::DW_FORM_addr,
308 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
309 : new (DIEValueAllocator) DIEInteger(0));
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000310}
311
Stephen Hines36b56882014-04-23 16:57:46 -0700312unsigned 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
323unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
324 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
325 : getCU().getOrCreateSourceID(FileName, DirName);
326}
327
Eric Christopher0969ddf2013-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///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700331void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
Eric Christopher0969ddf2013-01-18 22:11:33 +0000332 if (!DD->useSplitDwarf()) {
David Blaikie770530b2013-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 Christopher0969ddf2013-01-18 22:11:33 +0000335 } else {
David Blaikie770530b2013-10-21 17:28:37 +0000336 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700337 addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
338 DD->getAddressPool().getIndex(Sym));
Eric Christopher0969ddf2013-01-18 22:11:33 +0000339 }
340}
341
Stephen Hines36b56882014-04-23 16:57:46 -0700342/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel161b2f42011-04-12 23:21:44 +0000343///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700344void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700345 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel161b2f42011-04-12 23:21:44 +0000346 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700347 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
348 : dwarf::DW_FORM_data4,
349 Value);
Stephen Hines36b56882014-04-23 16:57:46 -0700350}
351
Stephen Hinesdce4a402014-05-29 02:49:00 -0700352void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700353 const MCSymbol *Hi, const MCSymbol *Lo) {
354 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700355 Die.addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000356}
357
358/// addDIEEntry - Add a DIE attribute data and value.
359///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700360void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
Manman Renb8b70e12013-10-31 17:54:35 +0000361 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
362}
363
Stephen Hinesdce4a402014-05-29 02:49:00 -0700364void DwarfUnit::addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type) {
365 // 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
371 Die.addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
372 new (DIEValueAllocator) DIETypeSignature(Type));
Stephen Hines36b56882014-04-23 16:57:46 -0700373}
374
Stephen Hinesdce4a402014-05-29 02:49:00 -0700375void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700376 DIEEntry *Entry) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700377 const DIE *DieCU = Die.getUnitOrNull();
378 const DIE *EntryCU = Entry->getEntry().getUnitOrNull();
Manman Renb8b70e12013-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700381 DieCU = &getUnitDie();
Manman Renb8b70e12013-10-31 17:54:35 +0000382 if (!EntryCU)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700383 EntryCU = &getUnitDie();
384 Die.addValue(Attribute,
385 EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
386 Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000387}
388
Manman Ren1a5e7872013-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700391DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
392 assert(Tag != dwarf::DW_TAG_auto_variable &&
393 Tag != dwarf::DW_TAG_arg_variable);
394 Parent.addChild(make_unique<DIE>((dwarf::Tag)Tag));
395 DIE &Die = *Parent.getChildren().back();
David Blaikie1dc27232013-11-16 00:29:01 +0000396 if (N)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700397 insertDIE(N, &Die);
Manman Ren1a5e7872013-10-29 00:53:03 +0000398 return Die;
399}
400
Devang Patel161b2f42011-04-12 23:21:44 +0000401/// addBlock - Add block data.
402///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700403void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Stephen Hines36b56882014-04-23 16:57:46 -0700404 Loc->ComputeSize(Asm);
405 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700406 Die.addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
Stephen Hines36b56882014-04-23 16:57:46 -0700407}
408
Stephen Hinesdce4a402014-05-29 02:49:00 -0700409void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700410 DIEBlock *Block) {
Devang Patel161b2f42011-04-12 23:21:44 +0000411 Block->ComputeSize(Asm);
412 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700413 Die.addValue(Attribute, Block->BestForm(), Block);
Devang Patel161b2f42011-04-12 23:21:44 +0000414}
415
416/// addSourceLine - Add location information to specified debug information
417/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700418void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File,
Stephen Hines36b56882014-04-23 16:57:46 -0700419 StringRef Directory) {
Devang Patel161b2f42011-04-12 23:21:44 +0000420 if (Line == 0)
421 return;
Stephen Hines36b56882014-04-23 16:57:46 -0700422
423 unsigned FileID = getOrCreateSourceID(File, Directory);
Devang Patel161b2f42011-04-12 23:21:44 +0000424 assert(FileID && "Invalid file id");
David Blaikie770530b2013-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 Patel161b2f42011-04-12 23:21:44 +0000427}
428
429/// addSourceLine - Add location information to specified debug information
430/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700431void DwarfUnit::addSourceLine(DIE &Die, DIVariable V) {
Stephen Hines36b56882014-04-23 16:57:46 -0700432 assert(V.isVariable());
Devang Patel161b2f42011-04-12 23:21:44 +0000433
Stephen Hines36b56882014-04-23 16:57:46 -0700434 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
435 V.getContext().getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000436}
437
438/// addSourceLine - Add location information to specified debug information
439/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700440void DwarfUnit::addSourceLine(DIE &Die, DIGlobalVariable G) {
Stephen Hines36b56882014-04-23 16:57:46 -0700441 assert(G.isGlobalVariable());
Eric Christopher2125d5a2012-03-15 23:55:40 +0000442
Stephen Hines36b56882014-04-23 16:57:46 -0700443 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000444}
445
446/// addSourceLine - Add location information to specified debug information
447/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700448void DwarfUnit::addSourceLine(DIE &Die, DISubprogram SP) {
Stephen Hines36b56882014-04-23 16:57:46 -0700449 assert(SP.isSubprogram());
Devang Patel161b2f42011-04-12 23:21:44 +0000450
Stephen Hines36b56882014-04-23 16:57:46 -0700451 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000452}
453
454/// addSourceLine - Add location information to specified debug information
455/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700456void DwarfUnit::addSourceLine(DIE &Die, DIType Ty) {
Stephen Hines36b56882014-04-23 16:57:46 -0700457 assert(Ty.isType());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000458
Stephen Hines36b56882014-04-23 16:57:46 -0700459 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
460}
461
462/// addSourceLine - Add location information to specified debug information
463/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700464void DwarfUnit::addSourceLine(DIE &Die, DIObjCProperty Ty) {
Stephen Hines36b56882014-04-23 16:57:46 -0700465 assert(Ty.isObjCProperty());
466
Eric Christopherb8ca9882012-03-29 08:42:56 +0000467 DIFile File = Ty.getFile();
Stephen Hines36b56882014-04-23 16:57:46 -0700468 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
469 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000470}
471
472/// addSourceLine - Add location information to specified debug information
473/// entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700474void DwarfUnit::addSourceLine(DIE &Die, DINameSpace NS) {
Stephen Hines36b56882014-04-23 16:57:46 -0700475 assert(NS.Verify());
Devang Patel161b2f42011-04-12 23:21:44 +0000476
Stephen Hines36b56882014-04-23 16:57:46 -0700477 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000478}
479
Eric Christopher8b4310b2012-11-21 00:34:38 +0000480/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000481/// DbgVariable based on provided MachineLocation.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700482void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
Stephen Hines36b56882014-04-23 16:57:46 -0700483 MachineLocation Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000484 if (DV.variableHasComplexAddress())
Devang Patel161b2f42011-04-12 23:21:44 +0000485 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherf61dbc12013-06-24 21:07:27 +0000486 else if (DV.isBlockByrefVariable())
Devang Patel161b2f42011-04-12 23:21:44 +0000487 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
488 else
David Blaikie4532c282013-06-20 00:25:24 +0000489 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherf61dbc12013-06-24 21:07:27 +0000490 DV.getVariable().isIndirect());
Devang Patel161b2f42011-04-12 23:21:44 +0000491}
492
Devang Patel116da2f2011-04-26 19:06:18 +0000493/// addRegisterOp - Add register operand.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700494void DwarfUnit::addRegisterOp(DIELoc &TheDie, unsigned Reg) {
Devang Patel116da2f2011-04-26 19:06:18 +0000495 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Stephen Hines36b56882014-04-23 16:57:46 -0700496 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) {
509 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
510 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
511 return;
512 }
513
514 // Emit register
Devang Patel116da2f2011-04-26 19:06:18 +0000515 if (DWReg < 32)
David Blaikie770530b2013-10-21 17:28:37 +0000516 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patel116da2f2011-04-26 19:06:18 +0000517 else {
David Blaikie770530b2013-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 Patel116da2f2011-04-26 19:06:18 +0000520 }
Stephen Hines36b56882014-04-23 16:57:46 -0700521
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 {
531 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
532 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
533 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
534 }
535 }
Devang Patel116da2f2011-04-26 19:06:18 +0000536}
537
538/// addRegisterOffset - Add register offset.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700539void DwarfUnit::addRegisterOffset(DIELoc &TheDie, unsigned Reg,
Stephen Hines36b56882014-04-23 16:57:46 -0700540 int64_t Offset) {
Devang Patel116da2f2011-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 Blaikie770530b2013-10-21 17:28:37 +0000546 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patel116da2f2011-04-26 19:06:18 +0000547 else if (DWReg < 32)
David Blaikie770530b2013-10-21 17:28:37 +0000548 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patel116da2f2011-04-26 19:06:18 +0000549 else {
David Blaikie770530b2013-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 Patel116da2f2011-04-26 19:06:18 +0000552 }
David Blaikie770530b2013-10-21 17:28:37 +0000553 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patel116da2f2011-04-26 19:06:18 +0000554}
555
556/// addAddress - Add an address attribute to a die based on the location
557/// provided.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700558void DwarfUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Stephen Hines36b56882014-04-23 16:57:46 -0700559 const MachineLocation &Location, bool Indirect) {
560 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel116da2f2011-04-26 19:06:18 +0000561
David Blaikie4532c282013-06-20 00:25:24 +0000562 if (Location.isReg() && !Indirect)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700563 addRegisterOp(*Loc, Location.getReg());
David Blaikie4532c282013-06-20 00:25:24 +0000564 else {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700565 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
David Blaikie4532c282013-06-20 00:25:24 +0000566 if (Indirect && !Location.isReg()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700567 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikie4532c282013-06-20 00:25:24 +0000568 }
569 }
Devang Patel116da2f2011-04-26 19:06:18 +0000570
571 // Now attach the location information to the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700572 addBlock(Die, Attribute, Loc);
Devang Patel116da2f2011-04-26 19:06:18 +0000573}
574
Devang Patel161b2f42011-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
Stephen Hines36b56882014-04-23 16:57:46 -0700577/// given the extra address information encoded in the DbgVariable, starting
578/// from the starting location. Add the DWARF information to the die.
Devang Patel161b2f42011-04-12 23:21:44 +0000579///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700580void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
Stephen Hines36b56882014-04-23 16:57:46 -0700581 dwarf::Attribute Attribute,
582 const MachineLocation &Location) {
583 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherf61dbc12013-06-24 21:07:27 +0000584 unsigned N = DV.getNumAddrElements();
Devang Patelc26f5442011-04-28 02:22:40 +0000585 unsigned i = 0;
586 if (Location.isReg()) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000587 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patelc26f5442011-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700590 addRegisterOffset(*Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patelc26f5442011-04-28 02:22:40 +0000591 i = 2;
592 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700593 addRegisterOp(*Loc, Location.getReg());
Eric Christopher6efc0432013-10-19 01:04:47 +0000594 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700595 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000596
Eric Christopher6efc0432013-10-19 01:04:47 +0000597 for (; i < N; ++i) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000598 uint64_t Element = DV.getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000599 if (Element == DIBuilder::OpPlus) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700600 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
601 addUInt(*Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel161b2f42011-04-12 23:21:44 +0000602 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000603 if (!Location.isReg())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700604 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopher6efc0432013-10-19 01:04:47 +0000605 } else
606 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel161b2f42011-04-12 23:21:44 +0000607 }
608
609 // Now attach the location information to the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700610 addBlock(Die, Attribute, Loc);
Devang Patel161b2f42011-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///
Stephen Hinesdce4a402014-05-29 02:49:00 -0700673void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
Stephen Hines36b56882014-04-23 16:57:46 -0700674 dwarf::Attribute Attribute,
675 const MachineLocation &Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000676 DIType Ty = DV.getType();
Devang Patel161b2f42011-04-12 23:21:44 +0000677 DIType TmpTy = Ty;
Eric Christopher31667622013-08-08 01:41:00 +0000678 uint16_t Tag = Ty.getTag();
Devang Patel161b2f42011-04-12 23:21:44 +0000679 bool isPointer = false;
680
Eric Christopherf61dbc12013-06-24 21:07:27 +0000681 StringRef varName = DV.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000682
683 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4adba522013-11-18 23:33:32 +0000684 DIDerivedType DTy(Ty);
Manman Ren017ceda2013-10-08 18:42:58 +0000685 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel161b2f42011-04-12 23:21:44 +0000686 isPointer = true;
687 }
688
David Blaikie4adba522013-11-18 23:33:32 +0000689 DICompositeType blockStruct(TmpTy);
Devang Patel161b2f42011-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 Blaikie4adba522013-11-18 23:33:32 +0000694 DIDerivedType varField;
695 DIDerivedType forwardingField;
Devang Patel161b2f42011-04-12 23:21:44 +0000696
697 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4adba522013-11-18 23:33:32 +0000698 DIDerivedType DT(Fields.getElement(i));
Devang Patel161b2f42011-04-12 23:21:44 +0000699 StringRef fieldName = DT.getName();
700 if (fieldName == "__forwarding")
David Blaikie4adba522013-11-18 23:33:32 +0000701 forwardingField = DT;
Devang Patel161b2f42011-04-12 23:21:44 +0000702 else if (fieldName == varName)
David Blaikie4adba522013-11-18 23:33:32 +0000703 varField = DT;
Devang Patel161b2f42011-04-12 23:21:44 +0000704 }
705
706 // Get the offsets for the forwarding field and the variable field.
David Blaikie4adba522013-11-18 23:33:32 +0000707 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
708 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel161b2f42011-04-12 23:21:44 +0000709
710 // Decode the original location, and use that as the start of the byref
711 // variable's location.
Stephen Hines36b56882014-04-23 16:57:46 -0700712 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel161b2f42011-04-12 23:21:44 +0000713
Eric Christophercaba2632012-07-04 02:02:18 +0000714 if (Location.isReg())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700715 addRegisterOp(*Loc, Location.getReg());
Eric Christophercaba2632012-07-04 02:02:18 +0000716 else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700717 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-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)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700722 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-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) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700728 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
729 addUInt(*Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel161b2f42011-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700734 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-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) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700740 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
741 addUInt(*Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel161b2f42011-04-12 23:21:44 +0000742 }
743
744 // Now attach the location information to the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700745 addBlock(Die, Attribute, Loc);
Devang Patel161b2f42011-04-12 23:21:44 +0000746}
747
Manman Renc664d762013-10-05 01:43:03 +0000748/// Return true if type encoding is unsigned.
749static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
750 DIDerivedType DTy(Ty);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700751 if (DTy.isDerivedType()) {
752 dwarf::Tag T = (dwarf::Tag)Ty.getTag();
753 // Encode pointer constants as unsigned bytes. This is used at least for
754 // 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.
758 if (T == dwarf::DW_TAG_pointer_type ||
759 T == dwarf::DW_TAG_ptr_to_member_type ||
760 T == dwarf::DW_TAG_reference_type ||
761 T == dwarf::DW_TAG_rvalue_reference_type)
762 return true;
763 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
764 T == dwarf::DW_TAG_volatile_type ||
765 T == dwarf::DW_TAG_restrict_type ||
766 T == dwarf::DW_TAG_enumeration_type);
767 if (DITypeRef Deriv = DTy.getTypeDerivedFrom())
768 return isUnsignedDIType(DD, DD->resolve(Deriv));
769 // 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 Renc664d762013-10-05 01:43:03 +0000774
775 DIBasicType BTy(Ty);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700776 assert(BTy.isBasicType());
777 unsigned Encoding = BTy.getEncoding();
778 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 ||
782 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean) &&
783 "Unsupported encoding");
784 return (Encoding == dwarf::DW_ATE_unsigned ||
785 Encoding == dwarf::DW_ATE_unsigned_char ||
786 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean);
Manman Renc664d762013-10-05 01:43:03 +0000787}
788
789/// If this type is derived from a base type then return base type size.
Manman Ren43251002013-10-08 18:46:58 +0000790static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renc664d762013-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
Stephen Hines36b56882014-04-23 16:57:46 -0700800 // 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 Renc664d762013-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 Ren43251002013-10-08 18:46:58 +0000813 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renc664d762013-10-05 01:43:03 +0000814
815 return BaseType.getSizeInBits();
816}
817
Devang Patel161b2f42011-04-12 23:21:44 +0000818/// addConstantFPValue - Add constant value entry in variable DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700819void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
Eric Christopher6efc0432013-10-19 01:04:47 +0000820 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-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 Christopher6efc0432013-10-19 01:04:47 +0000826 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000827
828 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000829 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-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)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700836 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel161b2f42011-04-12 23:21:44 +0000837
David Blaikie770530b2013-10-21 17:28:37 +0000838 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel161b2f42011-04-12 23:21:44 +0000839}
840
David Blaikie14268412013-01-20 01:18:01 +0000841/// addConstantFPValue - Add constant value entry in variable DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700842void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
Eric Christopher7b2ee392013-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 Blaikie14268412013-01-20 01:18:01 +0000845}
846
Devang Patel161b2f42011-04-12 23:21:44 +0000847/// addConstantValue - Add constant value entry in variable DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700848void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, DIType Ty) {
849 addConstantValue(Die, CI->getValue(), Ty);
850}
851
852/// 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
857 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm());
858}
859
860void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
861 // 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,
864 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
865}
866
867void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, DIType Ty) {
868 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty));
David Blaikie14268412013-01-20 01:18:01 +0000869}
870
871// addConstantValue - Add constant value entry in variable DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700872void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000873 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000874 if (CIBitWidth <= 64) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700875 addConstantValue(Die, Unsigned,
876 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
Eric Christophere4721492013-07-03 01:08:30 +0000877 return;
Devang Patel161b2f42011-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 Takumic3e48c32011-10-28 14:12:22 +0000883 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000884
885 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000886 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000887
888 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-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));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700895 addUInt(*Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000896 }
Devang Patel161b2f42011-04-12 23:21:44 +0000897
David Blaikie770530b2013-10-21 17:28:37 +0000898 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel161b2f42011-04-12 23:21:44 +0000899}
900
Eric Christopher6c3bb942013-04-22 07:47:40 +0000901/// addTemplateParams - Add template parameters into buffer.
Stephen Hines36b56882014-04-23 16:57:46 -0700902void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel161b2f42011-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 Renbe87b692013-10-23 23:05:28 +0000907 constructTemplateTypeParameterDIE(Buffer,
908 DITemplateTypeParameter(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000909 else if (Element.isTemplateValueParameter())
Manman Renbe87b692013-10-23 23:05:28 +0000910 constructTemplateValueParameterDIE(Buffer,
911 DITemplateValueParameter(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000912 }
Devang Patel161b2f42011-04-12 23:21:44 +0000913}
Nick Lewycky746cb672011-10-26 22:55:33 +0000914
Eric Christopher6b6061f2013-01-16 01:22:23 +0000915/// getOrCreateContextDIE - Get context owner's DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700916DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikie8c0fecc2013-11-14 21:24:34 +0000917 if (!Context || Context.isFile())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700918 return &getUnitDie();
Eric Christopher6b6061f2013-01-16 01:22:23 +0000919 if (Context.isType())
920 return getOrCreateTypeDIE(DIType(Context));
David Blaikief4837be2013-11-14 19:37:56 +0000921 if (Context.isNameSpace())
Eric Christopher6b6061f2013-01-16 01:22:23 +0000922 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikief4837be2013-11-14 19:37:56 +0000923 if (Context.isSubprogram())
Eric Christopher6b6061f2013-01-16 01:22:23 +0000924 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl0cbdb812013-11-15 21:05:09 +0000925 return getDIE(Context);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000926}
927
Stephen Hines36b56882014-04-23 16:57:46 -0700928DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
929 DIScope Context = resolve(Ty.getContext());
930 DIE *ContextDIE = getOrCreateContextDIE(Context);
931
Stephen Hinesdce4a402014-05-29 02:49:00 -0700932 if (DIE *TyDIE = getDIE(Ty))
Stephen Hines36b56882014-04-23 16:57:46 -0700933 return TyDIE;
934
935 // Create new type.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700936 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
Stephen Hines36b56882014-04-23 16:57:46 -0700937
Stephen Hinesdce4a402014-05-29 02:49:00 -0700938 constructTypeDIE(TyDIE, Ty);
Stephen Hines36b56882014-04-23 16:57:46 -0700939
940 updateAcceleratorTables(Context, Ty, TyDIE);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700941 return &TyDIE;
Stephen Hines36b56882014-04-23 16:57:46 -0700942}
943
Devang Patel161b2f42011-04-12 23:21:44 +0000944/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
945/// given DIType.
Stephen Hines36b56882014-04-23 16:57:46 -0700946DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie41e1a182013-11-14 22:25:02 +0000947 if (!TyNode)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700948 return nullptr;
Manman Rend498e5e2013-10-29 22:49:29 +0000949
David Blaikie41e1a182013-11-14 22:25:02 +0000950 DIType Ty(TyNode);
951 assert(Ty.isType());
Stephen Hines36b56882014-04-23 16:57:46 -0700952 assert(Ty == resolve(Ty.getRef()) &&
953 "type was not uniqued, possible ODR violation.");
David Blaikie41e1a182013-11-14 22:25:02 +0000954
Stephen Hinesdce4a402014-05-29 02:49:00 -0700955 // 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 Rend498e5e2013-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.
Stephen Hines36b56882014-04-23 16:57:46 -0700961 DIScope Context = resolve(Ty.getContext());
962 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl6bc810a2013-11-15 23:21:39 +0000963 assert(ContextDIE);
Manman Rend498e5e2013-10-29 22:49:29 +0000964
Stephen Hinesdce4a402014-05-29 02:49:00 -0700965 if (DIE *TyDIE = getDIE(Ty))
Devang Patel161b2f42011-04-12 23:21:44 +0000966 return TyDIE;
967
968 // Create new type.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700969 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
Manman Rend498e5e2013-10-29 22:49:29 +0000970
Stephen Hines36b56882014-04-23 16:57:46 -0700971 updateAcceleratorTables(Context, Ty, TyDIE);
972
Devang Patel161b2f42011-04-12 23:21:44 +0000973 if (Ty.isBasicType())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700974 constructTypeDIE(TyDIE, DIBasicType(Ty));
Stephen Hines36b56882014-04-23 16:57:46 -0700975 else if (Ty.isCompositeType()) {
976 DICompositeType CTy(Ty);
977 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
978 if (MDString *TypeId = CTy.getIdentifier()) {
979 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
980 // Skip updating the accelerator tables since this is not the full type.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700981 return &TyDIE;
Stephen Hines36b56882014-04-23 16:57:46 -0700982 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700983 constructTypeDIE(TyDIE, CTy);
Stephen Hines36b56882014-04-23 16:57:46 -0700984 } else {
Devang Patel161b2f42011-04-12 23:21:44 +0000985 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700986 constructTypeDIE(TyDIE, DIDerivedType(Ty));
Devang Patel161b2f42011-04-12 23:21:44 +0000987 }
Stephen Hines36b56882014-04-23 16:57:46 -0700988
Stephen Hinesdce4a402014-05-29 02:49:00 -0700989 return &TyDIE;
Stephen Hines36b56882014-04-23 16:57:46 -0700990}
991
992void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700993 const DIE &TyDIE) {
Eric Christopherc36145f2012-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 Christophere0167892012-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 Christopher6efc0432013-10-19 01:04:47 +00001000 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +00001001 }
Eric Christopher577056f2013-09-05 18:20:16 +00001002 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001003 DD->addAccelType(Ty.getName(), TyDIE, Flags);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001004
Stephen Hines36b56882014-04-23 16:57:46 -07001005 if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1006 Context.isNameSpace()) &&
1007 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001008 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] =
1009 &TyDIE;
Stephen Hines36b56882014-04-23 16:57:46 -07001010 }
Devang Patel161b2f42011-04-12 23:21:44 +00001011}
1012
1013/// addType - Add a new type attribute to the specified entity.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001014void DwarfUnit::addType(DIE &Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopherdc1363f2013-08-08 07:40:37 +00001015 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel161b2f42011-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 Renb8b70e12013-10-31 17:54:35 +00001021 addDIEEntry(Entity, Attribute, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +00001022 return;
1023 }
1024
1025 // Construct type.
1026 DIE *Buffer = getOrCreateTypeDIE(Ty);
1027
1028 // Set up proxy.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001029 Entry = createDIEEntry(*Buffer);
Devang Patel161b2f42011-04-12 23:21:44 +00001030 insertDIEEntry(Ty, Entry);
Manman Renb8b70e12013-10-31 17:54:35 +00001031 addDIEEntry(Entity, Attribute, Entry);
Devang Patel66658e42011-05-31 23:30:30 +00001032}
1033
Eric Christopher50d37a42013-09-20 22:20:55 +00001034/// addGlobalName - Add a new global name to the compile unit.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001035void DwarfUnit::addGlobalName(StringRef Name, DIE &Die, DIScope Context) {
Stephen Hines36b56882014-04-23 16:57:46 -07001036 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1037 return;
Eric Christopher0ab64392013-10-19 01:04:42 +00001038 std::string FullName = getParentContextString(Context) + Name.str();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001039 GlobalNames[FullName] = &Die;
Eric Christopher50d37a42013-09-20 22:20:55 +00001040}
1041
Eric Christopher91986572013-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.
Stephen Hines36b56882014-04-23 16:57:46 -07001047std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher91986572013-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 Christopher0ab64392013-10-19 01:04:42 +00001055 std::string CS;
Eric Christopher91986572013-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 Christopher0ab64392013-10-19 01:04:42 +00001074 if (!Name.empty()) {
Eric Christopher91986572013-10-17 02:06:06 +00001075 CS += Name;
1076 CS += "::";
1077 }
1078 }
1079 return CS;
Devang Patel161b2f42011-04-12 23:21:44 +00001080}
1081
1082/// constructTypeDIE - Construct basic type die from DIBasicType.
Stephen Hines36b56882014-04-23 16:57:46 -07001083void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001084 // Get core information.
1085 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +00001086 // Add name if not anonymous or intermediate type.
1087 if (!Name.empty())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001088 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +00001089
David Blaikie916d49e2013-10-04 23:21:16 +00001090 // An unspecified type only has a name attribute.
1091 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel734a67c2011-09-14 23:13:28 +00001092 return;
Devang Patel734a67c2011-09-14 23:13:28 +00001093
Stephen Hinesdce4a402014-05-29 02:49:00 -07001094 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +00001095 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +00001096
Devang Patel161b2f42011-04-12 23:21:44 +00001097 uint64_t Size = BTy.getSizeInBits() >> 3;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001098 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel161b2f42011-04-12 23:21:44 +00001099}
1100
1101/// constructTypeDIE - Construct derived type die from DIDerivedType.
Stephen Hines36b56882014-04-23 16:57:46 -07001102void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001103 // Get core information.
1104 StringRef Name = DTy.getName();
1105 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikie916d49e2013-10-04 23:21:16 +00001106 uint16_t Tag = Buffer.getTag();
Devang Patel161b2f42011-04-12 23:21:44 +00001107
1108 // Map to main type, void will not have a type.
Manman Ren017ceda2013-10-08 18:42:58 +00001109 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopherdc1363f2013-08-08 07:40:37 +00001110 if (FromTy)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001111 addType(Buffer, FromTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001112
1113 // Add name if not anonymous or intermediate type.
1114 if (!Name.empty())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001115 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001116
1117 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +00001118 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001119 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel161b2f42011-04-12 23:21:44 +00001120
David Blaikie62fdfb52013-01-07 05:51:15 +00001121 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001122 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
1123 *getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel161b2f42011-04-12 23:21:44 +00001124 // Add source line info if available and TyDesc is not a forward declaration.
1125 if (!DTy.isForwardDecl())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001126 addSourceLine(Buffer, DTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001127}
1128
Stephen Hines36b56882014-04-23 16:57:46 -07001129/// constructSubprogramArguments - Construct function argument DIEs.
1130void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
1131 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 {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001137 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Stephen Hines36b56882014-04-23 16:57:46 -07001138 addType(Arg, DIType(Ty));
1139 if (DIType(Ty).isArtificial())
1140 addFlag(Arg, dwarf::DW_AT_artificial);
1141 }
Eric Christopher3dee5752013-07-26 17:02:41 +00001142 }
1143}
1144
Devang Patel161b2f42011-04-12 23:21:44 +00001145/// constructTypeDIE - Construct type DIE from DICompositeType.
Stephen Hines36b56882014-04-23 16:57:46 -07001146void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1147 // Add name if not anonymous or intermediate type.
Devang Patel161b2f42011-04-12 23:21:44 +00001148 StringRef Name = CTy.getName();
1149
1150 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikie916d49e2013-10-04 23:21:16 +00001151 uint16_t Tag = Buffer.getTag();
Devang Patel161b2f42011-04-12 23:21:44 +00001152
1153 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +00001154 case dwarf::DW_TAG_array_type:
Eric Christopher883ed6b2013-11-11 18:52:31 +00001155 constructArrayTypeDIE(Buffer, CTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001156 break;
Eric Christopher95339892013-11-11 18:52:39 +00001157 case dwarf::DW_TAG_enumeration_type:
1158 constructEnumTypeDIE(Buffer, CTy);
1159 break;
Devang Patel161b2f42011-04-12 23:21:44 +00001160 case dwarf::DW_TAG_subroutine_type: {
Eric Christopherdc1363f2013-08-08 07:40:37 +00001161 // Add return type. A void return won't have a type.
Devang Patel161b2f42011-04-12 23:21:44 +00001162 DIArray Elements = CTy.getTypeArray();
David Blaikie4adba522013-11-18 23:33:32 +00001163 DIType RTy(Elements.getElement(0));
Eric Christopherdc1363f2013-08-08 07:40:37 +00001164 if (RTy)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001165 addType(Buffer, RTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001166
1167 bool isPrototyped = true;
Stephen Hines36b56882014-04-23 16:57:46 -07001168 if (Elements.getNumElements() == 2 &&
1169 Elements.getElement(1).isUnspecifiedParameter())
1170 isPrototyped = false;
1171
1172 constructSubprogramArguments(Buffer, Elements);
1173
Eric Christopher8b6fe6b2012-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 Blaikieaedaa722013-11-15 23:50:53 +00001176 uint16_t Language = getLanguage();
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001177 if (isPrototyped &&
Eric Christopher6efc0432013-10-19 01:04:47 +00001178 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher4d069bf2012-05-22 18:45:24 +00001179 Language == dwarf::DW_LANG_ObjC))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001180 addFlag(Buffer, dwarf::DW_AT_prototyped);
Stephen Hines36b56882014-04-23 16:57:46 -07001181
1182 if (CTy.isLValueReference())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001183 addFlag(Buffer, dwarf::DW_AT_reference);
Stephen Hines36b56882014-04-23 16:57:46 -07001184
1185 if (CTy.isRValueReference())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001186 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopher6efc0432013-10-19 01:04:47 +00001187 } break;
Devang Patel161b2f42011-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 Patel161b2f42011-04-12 23:21:44 +00001191 // Add elements to structure type.
David Blaikiec8b93552013-08-01 20:30:22 +00001192 DIArray Elements = CTy.getTypeArray();
1193 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +00001194 DIDescriptor Element = Elements.getElement(i);
Stephen Hines36b56882014-04-23 16:57:46 -07001195 if (Element.isSubprogram())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001196 getOrCreateSubprogramDIE(DISubprogram(Element));
Stephen Hines36b56882014-04-23 16:57:46 -07001197 else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +00001198 DIDerivedType DDTy(Element);
1199 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001200 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren017ceda2013-10-08 18:42:58 +00001201 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renc664d762013-10-05 01:43:03 +00001202 dwarf::DW_AT_friend);
Manman Ren655a10d2013-10-14 20:33:57 +00001203 } else if (DDTy.isStaticMember()) {
Manman Ren99addee2013-10-23 22:57:12 +00001204 getOrCreateStaticMemberDIE(DDTy);
Manman Ren655a10d2013-10-14 20:33:57 +00001205 } else {
Manman Rena1d25b62013-10-23 23:00:44 +00001206 constructMemberDIE(Buffer, DDTy);
Manman Ren655a10d2013-10-14 20:33:57 +00001207 }
Eric Christopher663e0cf2012-03-28 07:34:31 +00001208 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +00001209 DIObjCProperty Property(Element);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001210 DIE &ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Patel30d409c2012-02-07 23:33:58 +00001211 StringRef PropertyName = Property.getObjCPropertyName();
1212 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Stephen Hines36b56882014-04-23 16:57:46 -07001213 if (Property.getType())
1214 addType(ElemDie, Property.getType());
Eric Christopher4d069bf2012-05-22 18:45:24 +00001215 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-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 Patel9e11eb12012-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 Blaikie770530b2013-10-21 17:28:37 +00001236 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopher6efc0432013-10-19 01:04:47 +00001237 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001238
Devang Patel30d409c2012-02-07 23:33:58 +00001239 DIEEntry *Entry = getDIEEntry(Element);
1240 if (!Entry) {
1241 Entry = createDIEEntry(ElemDie);
1242 insertDIEEntry(Element, Entry);
1243 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001244 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001245 continue;
Devang Patel161b2f42011-04-12 23:21:44 +00001246 }
1247
1248 if (CTy.isAppleBlockExtension())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001249 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001250
Eric Christophereee74fb2013-10-05 00:27:02 +00001251 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4adba522013-11-18 23:33:32 +00001252 if (ContainingType)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001253 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
1254 *getOrCreateTypeDIE(ContainingType));
Devang Patel161b2f42011-04-12 23:21:44 +00001255
Devang Patel201e6cd2011-05-12 21:29:42 +00001256 if (CTy.isObjcClassComplete())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001257 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001258
Eric Christopher1a8e8862011-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 Christopher6efc0432013-10-19 01:04:47 +00001262 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-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())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001273 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001274
Eric Christopher4a5d8392012-05-22 18:45:18 +00001275 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopher6efc0432013-10-19 01:04:47 +00001276 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher4a5d8392012-05-22 18:45:18 +00001277 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001278 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001279 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001280 if (Size)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001281 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001282 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001283 // Add zero size if it is not a forward declaration.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001284 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001285
1286 // If we're a forward decl, say so.
1287 if (CTy.isForwardDecl())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001288 addFlag(Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001289
1290 // Add source line info if available.
1291 if (!CTy.isForwardDecl())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001292 addSourceLine(Buffer, CTy);
Eric Christopher89388952012-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)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001297 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
Eric Christopher6efc0432013-10-19 01:04:47 +00001298 RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001299 }
1300}
1301
Manman Renbe87b692013-10-23 23:05:28 +00001302/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1303/// DITemplateTypeParameter.
Stephen Hines36b56882014-04-23 16:57:46 -07001304void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1305 DITemplateTypeParameter TP) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001306 DIE &ParamDIE =
Manman Ren1a5e7872013-10-29 00:53:03 +00001307 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher76ef79f2013-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 Renb4d9c112013-10-09 19:46:28 +00001310 addType(ParamDIE, resolve(TP.getType()));
David Blaikiee88939c2013-06-22 18:59:11 +00001311 if (!TP.getName().empty())
1312 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001313}
1314
Manman Renbe87b692013-10-23 23:05:28 +00001315/// constructTemplateValueParameterDIE - Construct new DIE for the given
1316/// DITemplateValueParameter.
Manman Ren99addee2013-10-23 22:57:12 +00001317void
Stephen Hines36b56882014-04-23 16:57:46 -07001318DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1319 DITemplateValueParameter VP) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001320 DIE &ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopherdc1363f2013-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 Christopheref2d9192013-10-21 17:48:51 +00001324 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Renb4d9c112013-10-09 19:46:28 +00001325 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafdd1f82013-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 Blaikie4de9d722013-05-10 21:52:07 +00001329 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001330 addConstantValue(ParamDIE, CI, resolve(VP.getType()));
David Blaikie4de9d722013-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)
Stephen Hines36b56882014-04-23 16:57:46 -07001334 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001335 addOpAddress(*Loc, Asm->getSymbol(GV));
David Blaikie4de9d722013-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.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001338 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
Stephen Hines36b56882014-04-23 16:57:46 -07001339 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafdd1f82013-08-08 07:40:31 +00001340 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikiee88939c2013-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 Christopherafdd1f82013-08-08 07:40:31 +00001344 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikiee88939c2013-06-22 18:59:11 +00001345 assert(isa<MDNode>(Val));
1346 DIArray A(cast<MDNode>(Val));
Stephen Hinesdce4a402014-05-29 02:49:00 -07001347 addTemplateParams(ParamDIE, A);
David Blaikie4de9d722013-05-10 21:52:07 +00001348 }
1349 }
Devang Patel161b2f42011-04-12 23:21:44 +00001350}
1351
Devang Patel31c5d052011-05-06 16:57:54 +00001352/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Stephen Hines36b56882014-04-23 16:57:46 -07001353DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Ren6b713802013-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 Ren6b713802013-10-29 05:49:41 +00001357
Stephen Hinesdce4a402014-05-29 02:49:00 -07001358 if (DIE *NDie = getDIE(NS))
Devang Patel31c5d052011-05-06 16:57:54 +00001359 return NDie;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001360 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
Manman Ren6b713802013-10-29 05:49:41 +00001361
Eric Christopher09ac3d82011-11-07 09:24:32 +00001362 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001363 addString(NDie, dwarf::DW_AT_name, NS.getName());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001364 DD->addAccelNamespace(NS.getName(), NDie);
Eric Christopher91986572013-10-17 02:06:06 +00001365 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001366 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07001367 DD->addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001368 addSourceLine(NDie, NS);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001369 return &NDie;
Devang Patel31c5d052011-05-06 16:57:54 +00001370}
1371
Devang Pateldbc64af2011-08-15 17:24:54 +00001372/// getOrCreateSubprogramDIE - Create new DIE using SP.
Stephen Hines36b56882014-04-23 16:57:46 -07001373DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikiec32f2332013-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).
Stephen Hinesdce4a402014-05-29 02:49:00 -07001377 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
Stephen Hines36b56882014-04-23 16:57:46 -07001378
Stephen Hinesdce4a402014-05-29 02:49:00 -07001379 if (DIE *SPDie = getDIE(SP))
Devang Pateldbc64af2011-08-15 17:24:54 +00001380 return SPDie;
1381
Stephen Hinesdce4a402014-05-29 02:49:00 -07001382 if (DISubprogram SPDecl = SP.getFunctionDeclaration()) {
Manman Renbbdd02c2013-10-29 00:58:04 +00001383 // Add subprogram definitions to the CU die directly.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001384 ContextDIE = &getUnitDie();
1385 // Build the decl now to ensure it preceeds the definition.
1386 getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola01b55b42011-11-10 22:34:29 +00001387 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001388
Stephen Hinesdce4a402014-05-29 02:49:00 -07001389 // DW_TAG_inlined_subroutine may refer to this DIE.
1390 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1391
1392 // Abort here and fill this in later, depending on whether or not this
1393 // subprogram turns out to have inlined instances or not.
1394 if (SP.isDefinition())
1395 return &SPDie;
1396
1397 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()) {
1405 DeclDie = getDIE(SPDecl);
1406 assert(DeclDie);
1407 DeclLinkageName = SPDecl.getLinkageName();
1408 }
1409
1410 // Add function template parameters.
1411 addTemplateParams(SPDie, SP.getTemplateParams());
1412
1413 // Add the linkage name if we have one and it isn't in the Decl.
Michael Gottesmandc42d032013-09-04 04:39:38 +00001414 StringRef LinkageName = SP.getLinkageName();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001415 assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1416 LinkageName == DeclLinkageName) &&
1417 "decl has a linkage name and it is different");
1418 if (!LinkageName.empty() && DeclLinkageName.empty())
Eric Christophercbbd5b12012-08-23 22:52:55 +00001419 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001420 GlobalValue::getRealLinkageName(LinkageName));
Eric Christophercbbd5b12012-08-23 22:52:55 +00001421
Stephen Hinesdce4a402014-05-29 02:49:00 -07001422 if (DeclDie) {
1423 // Refer to the function declaration where all the other attributes will be
1424 // found.
1425 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1426 return;
1427 }
1428
Devang Pateldbc64af2011-08-15 17:24:54 +00001429 // Constructors and operators for anonymous aggregates do not have names.
1430 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001431 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001432
1433 addSourceLine(SPDie, SP);
1434
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001435 // Add the prototype if we have a prototype and we have a C like
1436 // language.
David Blaikieaedaa722013-11-15 23:50:53 +00001437 uint16_t Language = getLanguage();
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001438 if (SP.isPrototyped() &&
Eric Christopher6efc0432013-10-19 01:04:47 +00001439 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001440 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001441 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001442
Devang Pateldbc64af2011-08-15 17:24:54 +00001443 DICompositeType SPTy = SP.getType();
David Blaikie3d331842013-05-22 23:22:18 +00001444 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1445 "the type of a subprogram should be a subroutine");
Devang Pateldbc64af2011-08-15 17:24:54 +00001446
David Blaikie3d331842013-05-22 23:22:18 +00001447 DIArray Args = SPTy.getTypeArray();
Eric Christopheref2d9192013-10-21 17:48:51 +00001448 // Add a return type. If this is a type like a C/C++ void type we don't add a
1449 // return type.
Eric Christopherdc1363f2013-08-08 07:40:37 +00001450 if (Args.getElement(0))
1451 addType(SPDie, DIType(Args.getElement(0)));
Devang Pateldbc64af2011-08-15 17:24:54 +00001452
1453 unsigned VK = SP.getVirtuality();
1454 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001455 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Stephen Hines36b56882014-04-23 16:57:46 -07001456 DIELoc *Block = getDIELoc();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001457 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1458 addUInt(*Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
David Blaikie770530b2013-10-21 17:28:37 +00001459 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher61290022013-11-11 18:52:36 +00001460 ContainingTypeMap.insert(
Stephen Hinesdce4a402014-05-29 02:49:00 -07001461 std::make_pair(&SPDie, resolve(SP.getContainingType())));
Devang Pateldbc64af2011-08-15 17:24:54 +00001462 }
1463
1464 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001465 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001466
Devang Pateldbc64af2011-08-15 17:24:54 +00001467 // Add arguments. Do not add arguments for subprogram definition. They will
1468 // be handled while processing variables.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001469 constructSubprogramArguments(SPDie, Args);
Devang Pateldbc64af2011-08-15 17:24:54 +00001470 }
1471
1472 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001473 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001474
1475 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001476 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001477
1478 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001479 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001480
1481 if (unsigned isa = Asm->getISAEncoding()) {
1482 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1483 }
1484
Stephen Hines36b56882014-04-23 16:57:46 -07001485 if (SP.isLValueReference())
1486 addFlag(SPDie, dwarf::DW_AT_reference);
1487
1488 if (SP.isRValueReference())
1489 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1490
1491 if (SP.isProtected())
1492 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1493 dwarf::DW_ACCESS_protected);
1494 else if (SP.isPrivate())
1495 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1496 dwarf::DW_ACCESS_private);
1497 else
1498 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1499 dwarf::DW_ACCESS_public);
1500
1501 if (SP.isExplicit())
1502 addFlag(SPDie, dwarf::DW_AT_explicit);
Devang Pateldbc64af2011-08-15 17:24:54 +00001503}
1504
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001505// Return const expression if value is a GEP to access merged global
1506// constant. e.g.
1507// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1508static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1509 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1510 if (!CE || CE->getNumOperands() != 3 ||
1511 CE->getOpcode() != Instruction::GetElementPtr)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001512 return nullptr;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001513
1514 // First operand points to a global struct.
1515 Value *Ptr = CE->getOperand(0);
1516 if (!isa<GlobalValue>(Ptr) ||
1517 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001518 return nullptr;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001519
1520 // Second operand is zero.
1521 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1522 if (!CI || !CI->isZero())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001523 return nullptr;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001524
1525 // Third operand is offset.
1526 if (!isa<ConstantInt>(CE->getOperand(2)))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001527 return nullptr;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001528
1529 return CE;
1530}
1531
1532/// createGlobalVariableDIE - create global variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -07001533void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001534 // Check for pre-existence.
David Blaikiecbc85a22013-11-15 23:09:13 +00001535 if (getDIE(GV))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001536 return;
1537
Stephen Hines36b56882014-04-23 16:57:46 -07001538 assert(GV.isGlobalVariable());
Devang Patel28bea082011-08-18 23:17:55 +00001539
Eric Christopherccb66362013-10-04 23:49:26 +00001540 DIScope GVContext = GV.getContext();
Stephen Hines36b56882014-04-23 16:57:46 -07001541 DIType GTy = DD->resolve(GV.getType());
Eric Christopher6b6061f2013-01-16 01:22:23 +00001542
1543 // If this is a static data member definition, some attributes belong
1544 // to the declaration DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001545 DIE *VariableDIE = nullptr;
Manman Ren945e8282013-02-01 23:54:37 +00001546 bool IsStaticMember = false;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001547 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1548 if (SDMDecl.Verify()) {
1549 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1550 // We need the declaration DIE that is in the static member's class.
Manman Ren655a10d2013-10-14 20:33:57 +00001551 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Ren945e8282013-02-01 23:54:37 +00001552 IsStaticMember = true;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001553 }
1554
1555 // If this is not a static data member definition, create the variable
1556 // DIE and add the initial set of attributes to it.
1557 if (!VariableDIE) {
Manman Ren6b713802013-10-29 05:49:41 +00001558 // Construct the context before querying for the existence of the DIE in
1559 // case such construction creates the DIE.
1560 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Ren6b713802013-10-29 05:49:41 +00001561
Eric Christopher6b6061f2013-01-16 01:22:23 +00001562 // Add to map.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001563 VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001564
1565 // Add name and type.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001566 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1567 addType(*VariableDIE, GTy);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001568
1569 // Add scoping info.
Eric Christophera486f552013-10-16 01:37:49 +00001570 if (!GV.isLocalToUnit())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001571 addFlag(*VariableDIE, dwarf::DW_AT_external);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001572
1573 // Add line number info.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001574 addSourceLine(*VariableDIE, GV);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001575 }
1576
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001577 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001578 bool addToAccelTable = false;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001579 DIE *VariableSpecDIE = nullptr;
1580 bool isGlobalVariable = GV.getGlobal() != nullptr;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001581 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001582 addToAccelTable = true;
Stephen Hines36b56882014-04-23 16:57:46 -07001583 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindolaffc7dca2013-10-29 17:07:16 +00001584 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikie59eaa382013-06-28 20:05:11 +00001585 if (GV.getGlobal()->isThreadLocal()) {
1586 // FIXME: Make this work with -gsplit-dwarf.
1587 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1588 assert((PointerSize == 4 || PointerSize == 8) &&
1589 "Add support for other sizes if necessary");
1590 // Based on GCC's support for TLS:
David Blaikie8fed05e2013-07-01 23:55:52 +00001591 if (!DD->useSplitDwarf()) {
1592 // 1) Start with a constNu of the appropriate pointer size
Stephen Hinesdce4a402014-05-29 02:49:00 -07001593 addUInt(*Loc, dwarf::DW_FORM_data1,
David Blaikie8fed05e2013-07-01 23:55:52 +00001594 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton379f76e2013-10-07 18:39:18 +00001595 // 2) containing the (relocated) offset of the TLS variable
1596 // within the module's TLS block.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001597 addExpr(*Loc, dwarf::DW_FORM_udata,
Stephen Hines36b56882014-04-23 16:57:46 -07001598 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8fed05e2013-07-01 23:55:52 +00001599 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001600 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1601 addUInt(*Loc, dwarf::DW_FORM_udata,
1602 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
David Blaikie8fed05e2013-07-01 23:55:52 +00001603 }
Richard Mitton379f76e2013-10-07 18:39:18 +00001604 // 3) followed by a custom OP to make the debugger do a TLS lookup.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001605 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
Stephen Hines36b56882014-04-23 16:57:46 -07001606 } else {
1607 DD->addArangeLabel(SymbolCU(this, Sym));
Stephen Hinesdce4a402014-05-29 02:49:00 -07001608 addOpAddress(*Loc, Sym);
Stephen Hines36b56882014-04-23 16:57:46 -07001609 }
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001610 // Do not create specification DIE if context is either compile unit
1611 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001612 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren02d29672013-09-09 19:05:21 +00001613 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001614 // Create specification DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001615 VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie);
1616 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE);
1617 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001618 // A static member's declaration is already flagged as such.
1619 if (!SDMDecl.Verify())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001620 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001621 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001622 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001623 }
Michael Gottesmandc42d032013-09-04 04:39:38 +00001624 // Add the linkage name.
Eric Christopher6b6061f2013-01-16 01:22:23 +00001625 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmandc42d032013-09-04 04:39:38 +00001626 if (!LinkageName.empty())
Eric Christopher8d45a982013-02-27 23:49:47 +00001627 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1628 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1629 // TAG_variable.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001630 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE
1631 : *VariableDIE,
Stephen Hines36b56882014-04-23 16:57:46 -07001632 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1633 : dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001634 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8b4310b2012-11-21 00:34:38 +00001635 } else if (const ConstantInt *CI =
Eric Christopher6efc0432013-10-19 01:04:47 +00001636 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001637 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001638 // emitting AT_const_value multiple times, we only add AT_const_value when
1639 // it is not a static member.
1640 if (!IsStaticMember)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001641 addConstantValue(*VariableDIE, CI, GTy);
David Blaikie08e51e12013-11-17 21:55:13 +00001642 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001643 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001644 // GV is a merged global.
Stephen Hines36b56882014-04-23 16:57:46 -07001645 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001646 Value *Ptr = CE->getOperand(0);
Stephen Hines36b56882014-04-23 16:57:46 -07001647 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1648 DD->addArangeLabel(SymbolCU(this, Sym));
Stephen Hinesdce4a402014-05-29 02:49:00 -07001649 addOpAddress(*Loc, Sym);
1650 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopher6efc0432013-10-19 01:04:47 +00001651 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001652 addUInt(*Loc, dwarf::DW_FORM_udata,
Eric Christopher6efc0432013-10-19 01:04:47 +00001653 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Stephen Hinesdce4a402014-05-29 02:49:00 -07001654 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1655 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001656 }
1657
Eric Christopherd117fbb2011-11-11 01:55:22 +00001658 if (addToAccelTable) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001659 DIE &AddrDIE = VariableSpecDIE ? *VariableSpecDIE : *VariableDIE;
1660 DD->addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001661
Eric Christopherd117fbb2011-11-11 01:55:22 +00001662 // If the linkage name is different than the name, go ahead and output
1663 // that as well into the name table.
1664 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001665 DD->addAccelName(GV.getLinkageName(), AddrDIE);
Eric Christopherd117fbb2011-11-11 01:55:22 +00001666 }
Eric Christophera486f552013-10-16 01:37:49 +00001667
1668 if (!GV.isLocalToUnit())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001669 addGlobalName(GV.getName(),
1670 VariableSpecDIE ? *VariableSpecDIE : *VariableDIE,
Eric Christopher91986572013-10-17 02:06:06 +00001671 GV.getContext());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001672}
1673
Devang Patel161b2f42011-04-12 23:21:44 +00001674/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Stephen Hines36b56882014-04-23 16:57:46 -07001675void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001676 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1677 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001678
Bill Wendling222c2fd2012-12-06 07:38:10 +00001679 // The LowerBound value defines the lower bounds which is typically zero for
1680 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1681 // Count == -1 then the array is unbounded and we do not emit
1682 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1683 // Count == 0, then the array has zero elements in which case we do not emit
1684 // an upper bound.
1685 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001686 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001687 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001688
Bill Wendling6afe4782012-12-06 07:55:19 +00001689 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikie770530b2013-10-21 17:28:37 +00001690 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling222c2fd2012-12-06 07:38:10 +00001691
1692 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001693 // FIXME: An unbounded array should reference the expression that defines
1694 // the array.
Eric Christopher61290022013-11-11 18:52:36 +00001695 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1696 LowerBound + Count - 1);
Devang Patel161b2f42011-04-12 23:21:44 +00001697}
1698
1699/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Stephen Hines36b56882014-04-23 16:57:46 -07001700void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopher883ed6b2013-11-11 18:52:31 +00001701 if (CTy.isVector())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001702 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001703
Eric Christopherdc1363f2013-08-08 07:40:37 +00001704 // Emit the element type.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001705 addType(Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel161b2f42011-04-12 23:21:44 +00001706
1707 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001708 // FIXME: This type should be passed down from the front end
1709 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001710 DIE *IdxTy = getIndexTyDie();
1711 if (!IdxTy) {
Stephen Hines36b56882014-04-23 16:57:46 -07001712 // Construct an integer type to use for indexes.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001713 IdxTy = &createAndAddDIE(dwarf::DW_TAG_base_type, UnitDie);
1714 addString(*IdxTy, dwarf::DW_AT_name, "sizetype");
1715 addUInt(*IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1716 addUInt(*IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Stephen Hines36b56882014-04-23 16:57:46 -07001717 dwarf::DW_ATE_unsigned);
Devang Patel161b2f42011-04-12 23:21:44 +00001718 setIndexTyDie(IdxTy);
1719 }
1720
1721 // Add subranges to array type.
Eric Christopher883ed6b2013-11-11 18:52:31 +00001722 DIArray Elements = CTy.getTypeArray();
Devang Patel161b2f42011-04-12 23:21:44 +00001723 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1724 DIDescriptor Element = Elements.getElement(i);
1725 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1726 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1727 }
1728}
1729
Eric Christopher95339892013-11-11 18:52:39 +00001730/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Stephen Hines36b56882014-04-23 16:57:46 -07001731void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopher95339892013-11-11 18:52:39 +00001732 DIArray Elements = CTy.getTypeArray();
1733
1734 // Add enumerators to enumeration type.
1735 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4adba522013-11-18 23:33:32 +00001736 DIEnumerator Enum(Elements.getElement(i));
Eric Christopher95339892013-11-11 18:52:39 +00001737 if (Enum.isEnumerator()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001738 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4adba522013-11-18 23:33:32 +00001739 StringRef Name = Enum.getName();
Eric Christopher95339892013-11-11 18:52:39 +00001740 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4adba522013-11-18 23:33:32 +00001741 int64_t Value = Enum.getEnumValue();
Stephen Hines36b56882014-04-23 16:57:46 -07001742 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1743 Value);
Eric Christopher95339892013-11-11 18:52:39 +00001744 }
1745 }
1746 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1747 if (DTy) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001748 addType(Buffer, DTy);
1749 addFlag(Buffer, dwarf::DW_AT_enum_class);
Eric Christopher95339892013-11-11 18:52:39 +00001750 }
Devang Patel161b2f42011-04-12 23:21:44 +00001751}
1752
Devang Pateldbc64af2011-08-15 17:24:54 +00001753/// constructContainingTypeDIEs - Construct DIEs for types that contain
1754/// vtables.
Stephen Hines36b56882014-04-23 16:57:46 -07001755void DwarfUnit::constructContainingTypeDIEs() {
Devang Pateldbc64af2011-08-15 17:24:54 +00001756 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopher6efc0432013-10-19 01:04:47 +00001757 CE = ContainingTypeMap.end();
1758 CI != CE; ++CI) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001759 DIE &SPDie = *CI->first;
David Blaikiecbc85a22013-11-15 23:09:13 +00001760 DIDescriptor D(CI->second);
1761 if (!D)
Eric Christopher6efc0432013-10-19 01:04:47 +00001762 continue;
David Blaikiecbc85a22013-11-15 23:09:13 +00001763 DIE *NDie = getDIE(D);
Eric Christopher6efc0432013-10-19 01:04:47 +00001764 if (!NDie)
1765 continue;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001766 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
Devang Pateldbc64af2011-08-15 17:24:54 +00001767 }
1768}
1769
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001770/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001771std::unique_ptr<DIE> DwarfUnit::constructVariableDIE(DbgVariable &DV,
1772 bool Abstract) {
1773 auto D = constructVariableDIEImpl(DV, Abstract);
1774 DV.setDIE(*D);
1775 return D;
1776}
1777
1778std::unique_ptr<DIE> DwarfUnit::constructVariableDIEImpl(const DbgVariable &DV,
1779 bool Abstract) {
Eric Christopher7b641812013-11-15 01:43:19 +00001780 StringRef Name = DV.getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001781
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001782 // Define variable debug information entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001783 auto VariableDie = make_unique<DIE>(DV.getTag());
Eric Christopher7b641812013-11-15 01:43:19 +00001784 DbgVariable *AbsVar = DV.getAbstractVariable();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001785 if (AbsVar && AbsVar->getDIE())
1786 addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin, *AbsVar->getDIE());
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001787 else {
David Blaikie2883fd42013-08-19 03:34:03 +00001788 if (!Name.empty())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001789 addString(*VariableDie, dwarf::DW_AT_name, Name);
1790 addSourceLine(*VariableDie, DV.getVariable());
1791 addType(*VariableDie, DV.getType());
1792 if (DV.isArtificial())
1793 addFlag(*VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001794 }
1795
Stephen Hinesdce4a402014-05-29 02:49:00 -07001796 if (Abstract)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001797 return VariableDie;
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001798
1799 // Add variable address.
1800
Eric Christopher7b641812013-11-15 01:43:19 +00001801 unsigned Offset = DV.getDotDebugLocOffset();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001802 if (Offset != ~0U) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001803 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001804 return VariableDie;
1805 }
1806
Eric Christopher8cf5e742011-10-03 15:49:20 +00001807 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher7b641812013-11-15 01:43:19 +00001808 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie6d9dbd52013-06-16 20:34:15 +00001809 assert(DVInsn->getNumOperands() == 3);
1810 if (DVInsn->getOperand(0).isReg()) {
1811 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantlb2754912013-07-09 21:44:06 +00001812 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl35176402013-07-09 20:28:37 +00001813 if (DVInsn->getOperand(1).isImm()) {
Eric Christopher6efc0432013-10-19 01:04:47 +00001814 MachineLocation Location(RegOp.getReg(),
1815 DVInsn->getOperand(1).getImm());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001816 addVariableAddress(DV, *VariableDie, Location);
David Blaikie6d9dbd52013-06-16 20:34:15 +00001817 } else if (RegOp.getReg())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001818 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie6d9dbd52013-06-16 20:34:15 +00001819 } else if (DVInsn->getOperand(0).isImm())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001820 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie6d9dbd52013-06-16 20:34:15 +00001821 else if (DVInsn->getOperand(0).isFPImm())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001822 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
David Blaikie6d9dbd52013-06-16 20:34:15 +00001823 else if (DVInsn->getOperand(0).isCImm())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001824 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
1825 DV.getType());
Eric Christophere4721492013-07-03 01:08:30 +00001826
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001827 return VariableDie;
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001828 }
1829
Stephen Hinesdce4a402014-05-29 02:49:00 -07001830 // .. else use frame index.
1831 int FI = DV.getFrameIndex();
1832 if (FI != ~0) {
1833 unsigned FrameReg = 0;
1834 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1835 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1836 MachineLocation Location(FrameReg, Offset);
1837 addVariableAddress(DV, *VariableDie, Location);
1838 }
1839
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001840 return VariableDie;
1841}
1842
Manman Rena1d25b62013-10-23 23:00:44 +00001843/// constructMemberDIE - Construct member DIE from DIDerivedType.
Stephen Hines36b56882014-04-23 16:57:46 -07001844void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001845 DIE &MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel161b2f42011-04-12 23:21:44 +00001846 StringRef Name = DT.getName();
1847 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001848 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001849
Manman Ren017ceda2013-10-08 18:42:58 +00001850 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel161b2f42011-04-12 23:21:44 +00001851
1852 addSourceLine(MemberDie, DT);
1853
Eric Christopher6efc0432013-10-19 01:04:47 +00001854 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel161b2f42011-04-12 23:21:44 +00001855
1856 // For C++, virtual base classes are not at fixed offset. Use following
1857 // expression to extract appropriate offset from vtable.
1858 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1859
Stephen Hines36b56882014-04-23 16:57:46 -07001860 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001861 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1862 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1863 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1864 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1865 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1866 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1867 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel161b2f42011-04-12 23:21:44 +00001868
David Blaikie770530b2013-10-21 17:28:37 +00001869 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie9b933922013-11-01 00:25:45 +00001870 } else {
1871 uint64_t Size = DT.getSizeInBits();
1872 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1873 uint64_t OffsetInBytes;
1874
1875 if (Size != FieldSize) {
Stephen Hines36b56882014-04-23 16:57:46 -07001876 // Handle bitfield, assume bytes are 8 bits.
1877 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1878 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
David Blaikie9b933922013-11-01 00:25:45 +00001879
1880 uint64_t Offset = DT.getOffsetInBits();
1881 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1882 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1883 uint64_t FieldOffset = (HiMark - FieldSize);
1884 Offset -= FieldOffset;
1885
1886 // Maybe we need to work from the other end.
1887 if (Asm->getDataLayout().isLittleEndian())
1888 Offset = FieldSize - (Offset + Size);
1889 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1890
Stephen Hines36b56882014-04-23 16:57:46 -07001891 // Here DW_AT_data_member_location points to the anonymous
David Blaikie9b933922013-11-01 00:25:45 +00001892 // field that includes this bit field.
1893 OffsetInBytes = FieldOffset >> 3;
1894 } else
1895 // This is not a bitfield.
1896 OffsetInBytes = DT.getOffsetInBits() >> 3;
Stephen Hines36b56882014-04-23 16:57:46 -07001897
1898 if (DD->getDwarfVersion() <= 2) {
1899 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001900 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1901 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
Stephen Hines36b56882014-04-23 16:57:46 -07001902 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1903 } else
1904 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1905 OffsetInBytes);
David Blaikie9b933922013-11-01 00:25:45 +00001906 }
Devang Patel161b2f42011-04-12 23:21:44 +00001907
1908 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001909 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001910 dwarf::DW_ACCESS_protected);
1911 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001912 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001913 dwarf::DW_ACCESS_private);
1914 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001915 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001916 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001917 dwarf::DW_ACCESS_public);
1918 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001919 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001920 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001921
1922 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001923 if (MDNode *PNode = DT.getObjCProperty())
1924 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001925 MemberDie.addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1926 PropertyDie);
Devang Patel6588abf2012-02-06 17:49:43 +00001927
David Blaikie01bc2b32012-12-13 22:43:07 +00001928 if (DT.isArtificial())
1929 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +00001930}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001931
Manman Ren655a10d2013-10-14 20:33:57 +00001932/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Stephen Hines36b56882014-04-23 16:57:46 -07001933DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher6b6061f2013-01-16 01:22:23 +00001934 if (!DT.Verify())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001935 return nullptr;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001936
Manman Ren655a10d2013-10-14 20:33:57 +00001937 // Construct the context before querying for the existence of the DIE in case
1938 // such construction creates the DIE.
1939 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikie8c0fecc2013-11-14 21:24:34 +00001940 assert(dwarf::isType(ContextDIE->getTag()) &&
1941 "Static member should belong to a type.");
Manman Ren655a10d2013-10-14 20:33:57 +00001942
Stephen Hinesdce4a402014-05-29 02:49:00 -07001943 if (DIE *StaticMemberDIE = getDIE(DT))
Manman Ren655a10d2013-10-14 20:33:57 +00001944 return StaticMemberDIE;
1945
Stephen Hinesdce4a402014-05-29 02:49:00 -07001946 DIE &StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Ren655a10d2013-10-14 20:33:57 +00001947
Manman Ren017ceda2013-10-08 18:42:58 +00001948 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher6b6061f2013-01-16 01:22:23 +00001949
1950 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1951 addType(StaticMemberDIE, Ty);
1952 addSourceLine(StaticMemberDIE, DT);
1953 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1954 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1955
1956 // FIXME: We could omit private if the parent is a class_type, and
1957 // public if the parent is something else.
1958 if (DT.isProtected())
1959 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1960 dwarf::DW_ACCESS_protected);
1961 else if (DT.isPrivate())
1962 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1963 dwarf::DW_ACCESS_private);
1964 else
1965 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1966 dwarf::DW_ACCESS_public);
1967
1968 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Stephen Hinesdce4a402014-05-29 02:49:00 -07001969 addConstantValue(StaticMemberDIE, CI, Ty);
David Blaikie14268412013-01-20 01:18:01 +00001970 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1971 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001972
Stephen Hinesdce4a402014-05-29 02:49:00 -07001973 return &StaticMemberDIE;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001974}
David Blaikie1d361132013-10-30 20:42:41 +00001975
Stephen Hines36b56882014-04-23 16:57:46 -07001976void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const {
David Blaikie1d361132013-10-30 20:42:41 +00001977 Asm->OutStreamer.AddComment("DWARF version number");
1978 Asm->EmitInt16(DD->getDwarfVersion());
1979 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Stephen Hines36b56882014-04-23 16:57:46 -07001980 // We share one abbreviations table across all units so it's always at the
1981 // start of the section. Use a relocatable offset where needed to ensure
1982 // linking doesn't invalidate that offset.
1983 if (ASectionSym)
1984 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
1985 else
1986 // Use a constant value when no symbol is provided.
1987 Asm->EmitInt32(0);
David Blaikie1d361132013-10-30 20:42:41 +00001988 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1989 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1990}
Stephen Hines36b56882014-04-23 16:57:46 -07001991
1992void DwarfUnit::addRange(RangeSpan Range) {
1993 // Only add a range for this unit if we're emitting full debug.
1994 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) {
1995 // If we have no current ranges just add the range and return, otherwise,
1996 // check the current section and CU against the previous section and CU we
1997 // emitted into and the subprogram was contained within. If these are the
1998 // same then extend our current range, otherwise add this as a new range.
1999 if (CURanges.size() == 0 ||
2000 this != DD->getPrevCU() ||
2001 Asm->getCurrentSection() != DD->getPrevSection()) {
2002 CURanges.push_back(Range);
2003 return;
2004 }
2005
2006 assert(&(CURanges.back().getEnd()->getSection()) ==
2007 &(Range.getEnd()->getSection()) &&
2008 "We can only append to a range in the same section!");
2009 CURanges.back().setEnd(Range.getEnd());
2010 }
2011}
2012
2013void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2014 // Define start line table label for each Compile Unit.
2015 MCSymbol *LineTableStartSym =
2016 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
2017
Stephen Hinesdce4a402014-05-29 02:49:00 -07002018 stmtListIndex = UnitDie.getValues().size();
Stephen Hines36b56882014-04-23 16:57:46 -07002019
2020 // DW_AT_stmt_list is a offset of line number information for this
2021 // compile unit in debug_line section. For split dwarf this is
2022 // left in the skeleton CU and so not included.
2023 // The line table entries are not always emitted in assembly, so it
2024 // is not okay to use line_table_start here.
2025 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Stephen Hinesdce4a402014-05-29 02:49:00 -07002026 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym);
Stephen Hines36b56882014-04-23 16:57:46 -07002027 else
Stephen Hinesdce4a402014-05-29 02:49:00 -07002028 addSectionDelta(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
Stephen Hines36b56882014-04-23 16:57:46 -07002029 DwarfLineSectionSym);
2030}
2031
2032void DwarfCompileUnit::applyStmtList(DIE &D) {
2033 D.addValue(dwarf::DW_AT_stmt_list,
Stephen Hinesdce4a402014-05-29 02:49:00 -07002034 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
2035 UnitDie.getValues()[stmtListIndex]);
Stephen Hines36b56882014-04-23 16:57:46 -07002036}
2037
2038void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const {
2039 DwarfUnit::emitHeader(ASectionSym);
2040 Asm->OutStreamer.AddComment("Type Signature");
2041 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2042 Asm->OutStreamer.AddComment("Type DIE Offset");
2043 // In a skeleton type unit there is no type DIE so emit a zero offset.
2044 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2045 sizeof(Ty->getOffset()));
2046}
2047
2048void DwarfTypeUnit::initSection(const MCSection *Section) {
2049 assert(!this->Section);
2050 this->Section = Section;
2051 // Since each type unit is contained in its own COMDAT section, the begin
2052 // label and the section label are the same. Using the begin label emission in
2053 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2054 // the only other alternative of lazily constructing start-of-section labels
2055 // and storing a mapping in DwarfDebug (or AsmPrinter).
2056 this->SectionSym = this->LabelBegin =
2057 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2058 this->LabelEnd =
2059 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
Stephen Hines36b56882014-04-23 16:57:46 -07002060}