blob: b7bde972aa8a21f2bc7b0f15d20f5833327ea516 [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel0e821f42011-04-12 23:21:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher160522c2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel0e821f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
David Blaikie2c86a722013-12-02 19:33:15 +000014#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000016#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/APFloat.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000019#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/GlobalVariable.h"
22#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000024#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000025#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000026#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000029#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000032#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000033
34using namespace llvm;
35
Chandler Carruth1b9dde02014-04-22 02:02:50 +000036#define DEBUG_TYPE "dwarfdebug"
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
Craig Topper353eda42014-04-24 06:44:33 +000047 DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr),
48 Skeleton(nullptr) {
David Blaikie319a05f2013-12-02 19:33:10 +000049 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
50}
51
Eric Christopher4287a492013-12-09 23:57:44 +000052DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
53 AsmPrinter *A, DwarfDebug *DW,
54 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000055 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000056 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000057}
58
David Blaikie15632ae2014-02-12 00:31:30 +000059DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
David Blaikie4a2f95f2014-03-18 01:17:26 +000060 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU,
David Blaikie8287aff2014-03-18 02:13:23 +000061 MCDwarfDwoLineTable *SplitLineTable)
David Blaikie4a2f95f2014-03-18 01:17:26 +000062 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU),
63 SplitLineTable(SplitLineTable) {
64 if (SplitLineTable)
David Blaikie65a74662014-04-25 18:26:14 +000065 addSectionOffset(*UnitDie, dwarf::DW_AT_stmt_list, 0);
David Blaikie4a2f95f2014-03-18 01:17:26 +000066}
David Blaikie409dd9c2013-11-19 23:08:21 +000067
David Blaikie319a05f2013-12-02 19:33:10 +000068/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000069DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000070 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
71 DIEBlocks[j]->~DIEBlock();
Eric Christopher4a741042014-02-16 08:46:55 +000072 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
73 DIELocs[j]->~DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +000074}
75
76/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
77/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000078DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000079 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
80 return Value;
81}
82
Bill Wendling3495f9b2012-12-06 07:55:19 +000083/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000084/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000085int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000086 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000087 default:
88 break;
89
90 case dwarf::DW_LANG_C89:
91 case dwarf::DW_LANG_C99:
92 case dwarf::DW_LANG_C:
93 case dwarf::DW_LANG_C_plus_plus:
94 case dwarf::DW_LANG_ObjC:
95 case dwarf::DW_LANG_ObjC_plus_plus:
96 return 0;
97
98 case dwarf::DW_LANG_Fortran77:
99 case dwarf::DW_LANG_Fortran90:
100 case dwarf::DW_LANG_Fortran95:
101 return 1;
102
103 // The languages below have valid values only if the DWARF version >= 4.
104 case dwarf::DW_LANG_Java:
105 case dwarf::DW_LANG_Python:
106 case dwarf::DW_LANG_UPC:
107 case dwarf::DW_LANG_D:
108 if (dwarf::DWARF_VERSION >= 4)
109 return 0;
110 break;
111
112 case dwarf::DW_LANG_Ada83:
113 case dwarf::DW_LANG_Ada95:
114 case dwarf::DW_LANG_Cobol74:
115 case dwarf::DW_LANG_Cobol85:
116 case dwarf::DW_LANG_Modula2:
117 case dwarf::DW_LANG_Pascal83:
118 case dwarf::DW_LANG_PLI:
119 if (dwarf::DWARF_VERSION >= 4)
120 return 1;
121 break;
122 }
123
124 return -1;
125}
126
Manman Ren4dbdc902013-10-31 17:54:35 +0000127/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000128static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000129 // When the MDNode can be part of the type system, the DIE can be shared
130 // across CUs.
131 // Combining type units and cross-CU DIE sharing is lower value (since
132 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
133 // level already) but may be implementable for some value in projects
134 // building multiple independent libraries with LTO and then linking those
135 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000136 return (D.isType() ||
137 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000138 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000139}
140
141/// getDIE - Returns the debug information entry map slot for the
142/// specified debug variable. We delegate the request to DwarfDebug
143/// when the DIE for this MDNode can be shared across CUs. The mappings
144/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000145DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000146 if (isShareableAcrossCUs(D))
147 return DD->getDIE(D);
148 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000149}
150
151/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
152/// when the DIE for this MDNode can be shared across CUs. The mappings
153/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000154void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000155 if (isShareableAcrossCUs(Desc)) {
156 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000157 return;
158 }
David Blaikie2ad00162013-11-15 23:09:13 +0000159 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000160}
161
Eric Christopherbb69a272012-08-24 01:14:27 +0000162/// addFlag - Add a flag that is true.
David Blaikie65a74662014-04-25 18:26:14 +0000163void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000164 if (DD->getDwarfVersion() >= 4)
David Blaikie65a74662014-04-25 18:26:14 +0000165 Die.addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000166 else
David Blaikie65a74662014-04-25 18:26:14 +0000167 Die.addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000168}
169
Devang Patel0e821f42011-04-12 23:21:44 +0000170/// addUInt - Add an unsigned integer attribute data and value.
171///
David Blaikie65a74662014-04-25 18:26:14 +0000172void DwarfUnit::addUInt(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000173 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000174 if (!Form)
175 Form = DIEInteger::BestForm(false, Integer);
176 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
177 DIEInteger(Integer);
David Blaikie65a74662014-04-25 18:26:14 +0000178 Die.addValue(Attribute, *Form, Value);
David Blaikief2443192013-10-21 17:28:37 +0000179}
180
David Blaikie65a74662014-04-25 18:26:14 +0000181void DwarfUnit::addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000182 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000183}
184
185/// addSInt - Add an signed integer attribute data and value.
186///
David Blaikie65a74662014-04-25 18:26:14 +0000187void DwarfUnit::addSInt(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000188 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000189 if (!Form)
190 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000191 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikie65a74662014-04-25 18:26:14 +0000192 Die.addValue(Attribute, *Form, Value);
David Blaikief2443192013-10-21 17:28:37 +0000193}
194
David Blaikie65a74662014-04-25 18:26:14 +0000195void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000196 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000197 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000198}
199
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000200/// addString - Add a string attribute data and value. We always emit a
201/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000202/// more predictable sizes. In the case of split dwarf we emit an index
203/// into another table which gets us the static offset into the string
204/// table.
David Blaikie65a74662014-04-25 18:26:14 +0000205void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000206 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000207
208 if (!DD->useSplitDwarf())
209 return addLocalString(Die, Attribute, String);
210
211 unsigned idx = DU->getStringPoolIndex(String);
212 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000213 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
David Blaikie65a74662014-04-25 18:26:14 +0000214 Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000215}
216
217/// addLocalString - Add a string attribute data and value. This is guaranteed
218/// to be in the local string pool instead of indirected.
David Blaikie65a74662014-04-25 18:26:14 +0000219void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000220 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000221 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000222 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000223 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000224 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000225 else {
Eric Christophere698f532012-12-20 21:58:36 +0000226 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000227 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000228 }
Eric Christopher05893f42013-12-30 18:32:31 +0000229 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
David Blaikie65a74662014-04-25 18:26:14 +0000230 Die.addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000231}
232
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000233/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000234///
David Blaikie65a74662014-04-25 18:26:14 +0000235void DwarfUnit::addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000236 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikie65a74662014-04-25 18:26:14 +0000237 Die.addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000238}
239
Eric Christophera27220f2014-03-05 22:41:20 +0000240/// addLocationList - Add a Dwarf loclistptr attribute data and value.
241///
David Blaikie65a74662014-04-25 18:26:14 +0000242void DwarfUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera27220f2014-03-05 22:41:20 +0000243 unsigned Index) {
244 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index);
245 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
246 : dwarf::DW_FORM_data4;
David Blaikie65a74662014-04-25 18:26:14 +0000247 Die.addValue(Attribute, Form, Value);
Eric Christophera27220f2014-03-05 22:41:20 +0000248}
249
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000250/// addLabel - Add a Dwarf label attribute data and value.
251///
David Blaikie65a74662014-04-25 18:26:14 +0000252void DwarfUnit::addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000253 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000254 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
David Blaikie65a74662014-04-25 18:26:14 +0000255 Die.addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000256}
257
David Blaikie65a74662014-04-25 18:26:14 +0000258void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000259 addLabel(Die, (dwarf::Attribute)0, Form, Label);
260}
261
Eric Christopher33ff6972013-11-21 23:46:41 +0000262/// addSectionLabel - Add a Dwarf section label attribute data and value.
263///
David Blaikie65a74662014-04-25 18:26:14 +0000264void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000265 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000266 if (DD->getDwarfVersion() >= 4)
267 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
268 else
269 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
270}
271
272/// addSectionOffset - Add an offset into a section attribute data and value.
273///
David Blaikie65a74662014-04-25 18:26:14 +0000274void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000275 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000276 if (DD->getDwarfVersion() >= 4)
277 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
278 else
279 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
280}
281
Eric Christopher962c9082013-01-15 23:56:56 +0000282/// addLabelAddress - Add a dwarf label attribute data and value using
283/// DW_FORM_addr or DW_FORM_GNU_addr_index.
284///
David Blaikie65a74662014-04-25 18:26:14 +0000285void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000286 const MCSymbol *Label) {
287
288 if (!DD->useSplitDwarf())
289 return addLocalLabelAddress(Die, Attribute, Label);
290
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000291 if (Label)
292 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000293
David Blaikied75fb282014-04-23 21:20:10 +0000294 unsigned idx = DD->getAddressPool().getIndex(Label);
Eric Christopher384f3fe2014-03-20 19:16:16 +0000295 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
David Blaikie65a74662014-04-25 18:26:14 +0000296 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher384f3fe2014-03-20 19:16:16 +0000297}
298
David Blaikie65a74662014-04-25 18:26:14 +0000299void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000300 dwarf::Attribute Attribute,
301 const MCSymbol *Label) {
302 if (Label)
303 DD->addArangeLabel(SymbolCU(this, Label));
304
David Blaikie65a74662014-04-25 18:26:14 +0000305 Die.addValue(Attribute, dwarf::DW_FORM_addr,
306 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
307 : new (DIEValueAllocator) DIEInteger(0));
Eric Christopher962c9082013-01-15 23:56:56 +0000308}
309
David Blaikie0e8d4012014-03-17 23:53:25 +0000310unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
311 // If we print assembly, we can't separate .file entries according to
312 // compile units. Thus all files will belong to the default compile unit.
313
314 // FIXME: add a better feature test than hasRawTextSupport. Even better,
315 // extend .file to support this.
316 return Asm->OutStreamer.EmitDwarfFileDirective(
317 0, DirName, FileName,
318 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
319}
320
David Blaikie4a2f95f2014-03-18 01:17:26 +0000321unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
322 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
323 : getCU().getOrCreateSourceID(FileName, DirName);
324}
325
Eric Christophere9ec2452013-01-18 22:11:33 +0000326/// addOpAddress - Add a dwarf op address data and value using the
327/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
328///
David Blaikie65a74662014-04-25 18:26:14 +0000329void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000330 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000331 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
332 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000333 } else {
David Blaikief2443192013-10-21 17:28:37 +0000334 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
David Blaikiee226b082014-04-23 21:04:59 +0000335 addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
David Blaikied75fb282014-04-23 21:20:10 +0000336 DD->getAddressPool().getIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000337 }
338}
339
Eric Christopher33ff6972013-11-21 23:46:41 +0000340/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000341///
David Blaikie65a74662014-04-25 18:26:14 +0000342void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000343 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000344 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
David Blaikie65a74662014-04-25 18:26:14 +0000345 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
346 : dwarf::DW_FORM_data4,
347 Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000348}
349
David Blaikie65a74662014-04-25 18:26:14 +0000350void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
David Blaikie48b1bdc2014-03-07 01:30:55 +0000351 const MCSymbol *Hi, const MCSymbol *Lo) {
352 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
David Blaikie65a74662014-04-25 18:26:14 +0000353 Die.addValue(Attribute, dwarf::DW_FORM_data4, Value);
David Blaikie48b1bdc2014-03-07 01:30:55 +0000354}
355
Devang Patel0e821f42011-04-12 23:21:44 +0000356/// addDIEEntry - Add a DIE attribute data and value.
357///
David Blaikie65a74662014-04-25 18:26:14 +0000358void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000359 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
360}
361
David Blaikie65a74662014-04-25 18:26:14 +0000362void DwarfUnit::addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type) {
363 Die.addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
364 new (DIEValueAllocator) DIETypeSignature(Type));
David Blaikie47f615e2013-12-17 23:32:35 +0000365}
366
David Blaikie65a74662014-04-25 18:26:14 +0000367void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000368 DIEEntry *Entry) {
David Blaikie65a74662014-04-25 18:26:14 +0000369 const DIE *DieCU = Die.getUnitOrNull();
David Blaikie409dd9c2013-11-19 23:08:21 +0000370 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000371 if (!DieCU)
372 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000373 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000374 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000375 EntryCU = getUnitDie();
David Blaikie65a74662014-04-25 18:26:14 +0000376 Die.addValue(Attribute,
377 EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
378 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000379}
380
Manman Renb987e512013-10-29 00:53:03 +0000381/// Create a DIE with the given Tag, add the DIE to its parent, and
382/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000383DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
David Blaikieefc403b2014-04-12 02:24:04 +0000384 assert(Tag != dwarf::DW_TAG_auto_variable &&
385 Tag != dwarf::DW_TAG_arg_variable);
386 DIE *Die = new DIE((dwarf::Tag)Tag);
Manman Renb987e512013-10-29 00:53:03 +0000387 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000388 if (N)
389 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000390 return Die;
391}
392
Devang Patel0e821f42011-04-12 23:21:44 +0000393/// addBlock - Add block data.
394///
David Blaikie65a74662014-04-25 18:26:14 +0000395void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000396 Loc->ComputeSize(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000397 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
David Blaikie65a74662014-04-25 18:26:14 +0000398 Die.addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
Eric Christopher4a741042014-02-16 08:46:55 +0000399}
400
David Blaikie65a74662014-04-25 18:26:14 +0000401void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000402 DIEBlock *Block) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000403 Block->ComputeSize(Asm);
Devang Patel0e821f42011-04-12 23:21:44 +0000404 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
David Blaikie65a74662014-04-25 18:26:14 +0000405 Die.addValue(Attribute, Block->BestForm(), Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000406}
407
408/// addSourceLine - Add location information to specified debug information
409/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000410void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File,
David Blaikie101613e2014-02-12 00:11:25 +0000411 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000412 if (Line == 0)
413 return;
David Blaikie101613e2014-02-12 00:11:25 +0000414
David Blaikie4a2f95f2014-03-18 01:17:26 +0000415 unsigned FileID = getOrCreateSourceID(File, Directory);
Devang Patel0e821f42011-04-12 23:21:44 +0000416 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000417 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
418 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000419}
420
421/// addSourceLine - Add location information to specified debug information
422/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000423void DwarfUnit::addSourceLine(DIE &Die, DIVariable V) {
David Blaikie101613e2014-02-12 00:11:25 +0000424 assert(V.isVariable());
425
Eric Christopher89a575c2014-02-12 22:38:04 +0000426 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
427 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000428}
429
430/// addSourceLine - Add location information to specified debug information
431/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000432void DwarfUnit::addSourceLine(DIE &Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000433 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000434
David Blaikie101613e2014-02-12 00:11:25 +0000435 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000436}
437
438/// addSourceLine - Add location information to specified debug information
439/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000440void DwarfUnit::addSourceLine(DIE &Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000441 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000442
David Blaikie101613e2014-02-12 00:11:25 +0000443 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000444}
445
446/// addSourceLine - Add location information to specified debug information
447/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000448void DwarfUnit::addSourceLine(DIE &Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000449 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000450
David Blaikie101613e2014-02-12 00:11:25 +0000451 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000452}
453
454/// addSourceLine - Add location information to specified debug information
455/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000456void DwarfUnit::addSourceLine(DIE &Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000457 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000458
Eric Christopher70e1bd82012-03-29 08:42:56 +0000459 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000460 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
461 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000462}
463
464/// addSourceLine - Add location information to specified debug information
465/// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000466void DwarfUnit::addSourceLine(DIE &Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000467 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000468
David Blaikie101613e2014-02-12 00:11:25 +0000469 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000470}
471
Eric Christopher92331fd2012-11-21 00:34:38 +0000472/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000473/// DbgVariable based on provided MachineLocation.
David Blaikie65a74662014-04-25 18:26:14 +0000474void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera5a79422013-12-09 23:32:48 +0000475 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000476 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000477 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000478 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000479 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
480 else
David Blaikieea2605d2013-06-20 00:25:24 +0000481 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000482 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000483}
484
Devang Patelba5fbf12011-04-26 19:06:18 +0000485/// addRegisterOp - Add register operand.
David Blaikie65a74662014-04-25 18:26:14 +0000486void DwarfUnit::addRegisterOp(DIELoc &TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000487 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000488 int DWReg = RI->getDwarfRegNum(Reg, false);
489 bool isSubRegister = DWReg < 0;
490
491 unsigned Idx = 0;
492
493 // Go up the super-register chain until we hit a valid dwarf register number.
494 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
495 DWReg = RI->getDwarfRegNum(*SR, false);
496 if (DWReg >= 0)
497 Idx = RI->getSubRegIndex(*SR, Reg);
498 }
499
500 if (DWReg < 0) {
Eric Christophera13839f2014-02-26 23:27:16 +0000501 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000502 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
503 return;
504 }
505
506 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000507 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000508 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000509 else {
David Blaikief2443192013-10-21 17:28:37 +0000510 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
511 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000512 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000513
514 // Emit Mask
515 if (isSubRegister) {
516 unsigned Size = RI->getSubRegIdxSize(Idx);
517 unsigned Offset = RI->getSubRegIdxOffset(Idx);
518 if (Offset > 0) {
519 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
520 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
521 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
522 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000523 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000524 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000525 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000526 }
527 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000528}
529
530/// addRegisterOffset - Add register offset.
David Blaikie65a74662014-04-25 18:26:14 +0000531void DwarfUnit::addRegisterOffset(DIELoc &TheDie, unsigned Reg,
Eric Christophera5a79422013-12-09 23:32:48 +0000532 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000533 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
534 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
535 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
536 if (Reg == TRI->getFrameRegister(*Asm->MF))
537 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000538 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000539 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000540 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000541 else {
David Blaikief2443192013-10-21 17:28:37 +0000542 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
543 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000544 }
David Blaikief2443192013-10-21 17:28:37 +0000545 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000546}
547
548/// addAddress - Add an address attribute to a die based on the location
549/// provided.
David Blaikie65a74662014-04-25 18:26:14 +0000550void DwarfUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera5a79422013-12-09 23:32:48 +0000551 const MachineLocation &Location, bool Indirect) {
Eric Christopher4a741042014-02-16 08:46:55 +0000552 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patelba5fbf12011-04-26 19:06:18 +0000553
David Blaikieea2605d2013-06-20 00:25:24 +0000554 if (Location.isReg() && !Indirect)
David Blaikie65a74662014-04-25 18:26:14 +0000555 addRegisterOp(*Loc, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000556 else {
David Blaikie65a74662014-04-25 18:26:14 +0000557 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000558 if (Indirect && !Location.isReg()) {
David Blaikie65a74662014-04-25 18:26:14 +0000559 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000560 }
561 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000562
563 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000564 addBlock(Die, Attribute, Loc);
Devang Patelba5fbf12011-04-26 19:06:18 +0000565}
566
Devang Patel0e821f42011-04-12 23:21:44 +0000567/// addComplexAddress - Start with the address based on the location provided,
568/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000569/// given the extra address information encoded in the DbgVariable, starting
570/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000571///
David Blaikie65a74662014-04-25 18:26:14 +0000572void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera5a79422013-12-09 23:32:48 +0000573 dwarf::Attribute Attribute,
574 const MachineLocation &Location) {
Eric Christopher4a741042014-02-16 08:46:55 +0000575 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000576 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000577 unsigned i = 0;
578 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000579 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000580 // If first address element is OpPlus then emit
581 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
David Blaikie65a74662014-04-25 18:26:14 +0000582 addRegisterOffset(*Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000583 i = 2;
584 } else
David Blaikie65a74662014-04-25 18:26:14 +0000585 addRegisterOp(*Loc, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000586 } else
David Blaikie65a74662014-04-25 18:26:14 +0000587 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000588
Eric Christopherc2697f82013-10-19 01:04:47 +0000589 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000590 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000591 if (Element == DIBuilder::OpPlus) {
David Blaikie65a74662014-04-25 18:26:14 +0000592 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
593 addUInt(*Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000594 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000595 if (!Location.isReg())
David Blaikie65a74662014-04-25 18:26:14 +0000596 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000597 } else
598 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000599 }
600
601 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000602 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000603}
604
605/* Byref variables, in Blocks, are declared by the programmer as "SomeType
606 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
607 gives the variable VarName either the struct, or a pointer to the struct, as
608 its type. This is necessary for various behind-the-scenes things the
609 compiler needs to do with by-reference variables in Blocks.
610
611 However, as far as the original *programmer* is concerned, the variable
612 should still have type 'SomeType', as originally declared.
613
614 The function getBlockByrefType dives into the __Block_byref_x_VarName
615 struct to find the original type of the variable, which is then assigned to
616 the variable's Debug Information Entry as its real type. So far, so good.
617 However now the debugger will expect the variable VarName to have the type
618 SomeType. So we need the location attribute for the variable to be an
619 expression that explains to the debugger how to navigate through the
620 pointers and struct to find the actual variable of type SomeType.
621
622 The following function does just that. We start by getting
623 the "normal" location for the variable. This will be the location
624 of either the struct __Block_byref_x_VarName or the pointer to the
625 struct __Block_byref_x_VarName.
626
627 The struct will look something like:
628
629 struct __Block_byref_x_VarName {
630 ... <various fields>
631 struct __Block_byref_x_VarName *forwarding;
632 ... <various other fields>
633 SomeType VarName;
634 ... <maybe more fields>
635 };
636
637 If we are given the struct directly (as our starting point) we
638 need to tell the debugger to:
639
640 1). Add the offset of the forwarding field.
641
642 2). Follow that pointer to get the real __Block_byref_x_VarName
643 struct to use (the real one may have been copied onto the heap).
644
645 3). Add the offset for the field VarName, to find the actual variable.
646
647 If we started with a pointer to the struct, then we need to
648 dereference that pointer first, before the other steps.
649 Translating this into DWARF ops, we will need to append the following
650 to the current location description for the variable:
651
652 DW_OP_deref -- optional, if we start with a pointer
653 DW_OP_plus_uconst <forward_fld_offset>
654 DW_OP_deref
655 DW_OP_plus_uconst <varName_fld_offset>
656
657 That is what this function does. */
658
659/// addBlockByrefAddress - Start with the address based on the location
660/// provided, and generate the DWARF information necessary to find the
661/// actual Block variable (navigating the Block struct) based on the
662/// starting location. Add the DWARF information to the die. For
663/// more information, read large comment just above here.
664///
David Blaikie65a74662014-04-25 18:26:14 +0000665void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera5a79422013-12-09 23:32:48 +0000666 dwarf::Attribute Attribute,
667 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000668 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000669 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000670 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000671 bool isPointer = false;
672
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000673 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000674
675 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000676 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000677 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000678 isPointer = true;
679 }
680
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000681 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000682
683 // Find the __forwarding field and the variable field in the __Block_byref
684 // struct.
685 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000686 DIDerivedType varField;
687 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000688
689 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000690 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000691 StringRef fieldName = DT.getName();
692 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000693 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000694 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000695 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000696 }
697
698 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000699 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
700 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000701
702 // Decode the original location, and use that as the start of the byref
703 // variable's location.
Eric Christopher4a741042014-02-16 08:46:55 +0000704 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +0000705
Eric Christopheref9d7102012-07-04 02:02:18 +0000706 if (Location.isReg())
David Blaikie65a74662014-04-25 18:26:14 +0000707 addRegisterOp(*Loc, Location.getReg());
Eric Christopheref9d7102012-07-04 02:02:18 +0000708 else
David Blaikie65a74662014-04-25 18:26:14 +0000709 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000710
711 // If we started with a pointer to the __Block_byref... struct, then
712 // the first thing we need to do is dereference the pointer (DW_OP_deref).
713 if (isPointer)
David Blaikie65a74662014-04-25 18:26:14 +0000714 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000715
716 // Next add the offset for the '__forwarding' field:
717 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
718 // adding the offset if it's 0.
719 if (forwardingFieldOffset > 0) {
David Blaikie65a74662014-04-25 18:26:14 +0000720 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
721 addUInt(*Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000722 }
723
724 // Now dereference the __forwarding field to get to the real __Block_byref
725 // struct: DW_OP_deref.
David Blaikie65a74662014-04-25 18:26:14 +0000726 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000727
728 // Now that we've got the real __Block_byref... struct, add the offset
729 // for the variable's field to get to the location of the actual variable:
730 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
731 if (varFieldOffset > 0) {
David Blaikie65a74662014-04-25 18:26:14 +0000732 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
733 addUInt(*Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000734 }
735
736 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000737 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000738}
739
Devang Patelbcd50a12011-07-20 21:57:04 +0000740/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000741static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000742 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000743 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
744 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000745 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000746 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
747 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000748 *SizeInBits = Ty.getSizeInBits();
749 return true;
750 }
751 return false;
752}
753
Manman Renb3388602013-10-05 01:43:03 +0000754/// Return true if type encoding is unsigned.
755static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
756 DIDerivedType DTy(Ty);
757 if (DTy.isDerivedType())
758 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
759
760 DIBasicType BTy(Ty);
761 if (BTy.isBasicType()) {
762 unsigned Encoding = BTy.getEncoding();
763 if (Encoding == dwarf::DW_ATE_unsigned ||
764 Encoding == dwarf::DW_ATE_unsigned_char ||
765 Encoding == dwarf::DW_ATE_boolean)
766 return true;
767 }
768 return false;
769}
770
771/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000772static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000773 unsigned Tag = Ty.getTag();
774
775 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
776 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
777 Tag != dwarf::DW_TAG_restrict_type)
778 return Ty.getSizeInBits();
779
780 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
781
Eric Christopher8cc04fc2014-03-12 18:18:05 +0000782 // If this type is not derived from any type or the type is a declaration then
783 // take conservative approach.
784 if (!BaseType.isValid() || BaseType.isForwardDecl())
Manman Renb3388602013-10-05 01:43:03 +0000785 return Ty.getSizeInBits();
786
787 // If this is a derived type, go ahead and get the base type, unless it's a
788 // reference then it's just the size of the field. Pointer types have no need
789 // of this since they're a different type of qualification on the type.
790 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
791 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
792 return Ty.getSizeInBits();
793
794 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000795 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000796
797 return BaseType.getSizeInBits();
798}
799
Devang Patel0e821f42011-04-12 23:21:44 +0000800/// addConstantValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000801void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO,
Eric Christophera5a79422013-12-09 23:32:48 +0000802 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000803 // FIXME: This is a bit conservative/simple - it emits negative values at
804 // their maximum bit width which is a bit unfortunate (& doesn't prefer
805 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000806 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000807 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000808 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000809 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000810
Eric Christopher9d1daa82013-08-27 23:49:04 +0000811 // If we're a signed constant definitely use sdata.
812 if (SignedConstant) {
813 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
814 return;
815 }
816
817 // Else use data for now unless it's larger than we can deal with.
818 switch (SizeInBits) {
819 case 8:
820 Form = dwarf::DW_FORM_data1;
821 break;
822 case 16:
823 Form = dwarf::DW_FORM_data2;
824 break;
825 case 32:
826 Form = dwarf::DW_FORM_data4;
827 break;
828 case 64:
829 Form = dwarf::DW_FORM_data8;
830 break;
831 default:
832 Form = dwarf::DW_FORM_udata;
833 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
834 return;
835 }
836 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000837}
838
839/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000840void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000841 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000842 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
843 APFloat FPImm = MO.getFPImm()->getValueAPF();
844
845 // Get the raw data form of the floating point.
846 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000847 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000848
849 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000850 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000851 int Incr = (LittleEndian ? 1 : -1);
852 int Start = (LittleEndian ? 0 : NumBytes - 1);
853 int Stop = (LittleEndian ? NumBytes : -1);
854
855 // Output the constant to DWARF one byte at a time.
856 for (; Start != Stop; Start += Incr)
David Blaikie65a74662014-04-25 18:26:14 +0000857 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000858
David Blaikief2443192013-10-21 17:28:37 +0000859 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000860}
861
David Blaikiea39a76e2013-01-20 01:18:01 +0000862/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000863void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000864 // Pass this down to addConstantValue as an unsigned bag of bits.
865 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000866}
867
Devang Patel0e821f42011-04-12 23:21:44 +0000868/// addConstantValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000869void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
Eric Christophera5a79422013-12-09 23:32:48 +0000870 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000871 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000872}
873
874// addConstantValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000875void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000876 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000877 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000878 // If we're a signed constant definitely use sdata.
879 if (!Unsigned) {
880 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
881 Val.getSExtValue());
882 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000883 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000884
885 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000886 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000887 switch (CIBitWidth) {
888 case 8:
889 Form = dwarf::DW_FORM_data1;
890 break;
891 case 16:
892 Form = dwarf::DW_FORM_data2;
893 break;
894 case 32:
895 Form = dwarf::DW_FORM_data4;
896 break;
897 case 64:
898 Form = dwarf::DW_FORM_data8;
899 break;
900 default:
901 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
902 Val.getZExtValue());
903 return;
904 }
905 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000906 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000907 }
908
909 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
910
911 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000912 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000913
914 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000915 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000916
917 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000918 for (int i = 0; i < NumBytes; i++) {
919 uint8_t c;
920 if (LittleEndian)
921 c = Ptr64[i / 8] >> (8 * (i & 7));
922 else
923 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikie65a74662014-04-25 18:26:14 +0000924 addUInt(*Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000925 }
Devang Patel0e821f42011-04-12 23:21:44 +0000926
David Blaikief2443192013-10-21 17:28:37 +0000927 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000928}
929
Eric Christopher25e35092013-04-22 07:47:40 +0000930/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000931void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000932 // Add template parameters.
933 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
934 DIDescriptor Element = TParams.getElement(i);
935 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000936 constructTemplateTypeParameterDIE(Buffer,
937 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000938 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000939 constructTemplateValueParameterDIE(Buffer,
940 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000941 }
Devang Patel0e821f42011-04-12 23:21:44 +0000942}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000943
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000944/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000945DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000946 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000947 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000948 if (Context.isType())
949 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000950 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000951 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000952 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000953 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000954 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000955}
956
Eric Christophera5a79422013-12-09 23:32:48 +0000957DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000958 DIScope Context = resolve(Ty.getContext());
959 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000960
961 DIE *TyDIE = getDIE(Ty);
962 if (TyDIE)
963 return TyDIE;
964
965 // Create new type.
966 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
967
David Blaikiefbd29eb2013-11-26 00:35:04 +0000968 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000969
David Blaikie9d861be2013-11-26 00:15:27 +0000970 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000971 return TyDIE;
972}
973
Devang Patel0e821f42011-04-12 23:21:44 +0000974/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
975/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000976DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000977 if (!TyNode)
Craig Topper353eda42014-04-24 06:44:33 +0000978 return nullptr;
Manman Renf4c339e2013-10-29 22:49:29 +0000979
David Blaikie32887552013-11-14 22:25:02 +0000980 DIType Ty(TyNode);
981 assert(Ty.isType());
Adrian Prantlc95ec912014-03-24 21:33:01 +0000982 assert(Ty == resolve(Ty.getRef()) &&
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000983 "type was not uniqued, possible ODR violation.");
David Blaikie32887552013-11-14 22:25:02 +0000984
David Blaikieee11f222014-04-12 05:35:59 +0000985 // DW_TAG_restrict_type is not supported in DWARF2
986 if (Ty.getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
987 return getOrCreateTypeDIE(resolve(DIDerivedType(Ty).getTypeDerivedFrom()));
988
Manman Renf4c339e2013-10-29 22:49:29 +0000989 // Construct the context before querying for the existence of the DIE in case
990 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000991 DIScope Context = resolve(Ty.getContext());
992 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000993 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000994
David Blaikie65a74662014-04-25 18:26:14 +0000995 if (DIE *TyDIE = getDIE(Ty))
Devang Patel0e821f42011-04-12 23:21:44 +0000996 return TyDIE;
997
998 // Create new type.
David Blaikie65a74662014-04-25 18:26:14 +0000999 DIE &TyDIE = *createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
Manman Renf4c339e2013-10-29 22:49:29 +00001000
David Blaikie65a74662014-04-25 18:26:14 +00001001 updateAcceleratorTables(Context, Ty, &TyDIE);
David Blaikiec3d9e9e2014-03-06 01:42:00 +00001002
Devang Patel0e821f42011-04-12 23:21:44 +00001003 if (Ty.isBasicType())
David Blaikie65a74662014-04-25 18:26:14 +00001004 constructTypeDIE(TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +00001005 else if (Ty.isCompositeType()) {
1006 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +00001007 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
1008 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +00001009 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
Adrian Prantle2da17f2014-03-14 23:08:17 +00001010 // Skip updating the accelerator tables since this is not the full type.
David Blaikie65a74662014-04-25 18:26:14 +00001011 return &TyDIE;
David Blaikiecfb21152014-01-03 18:59:42 +00001012 }
David Blaikie65a74662014-04-25 18:26:14 +00001013 constructTypeDIE(TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +00001014 } else {
Devang Patel0e821f42011-04-12 23:21:44 +00001015 assert(Ty.isDerivedType() && "Unknown kind of DIType");
David Blaikie65a74662014-04-25 18:26:14 +00001016 constructTypeDIE(TyDIE, DIDerivedType(Ty));
Devang Patel0e821f42011-04-12 23:21:44 +00001017 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001018
David Blaikie65a74662014-04-25 18:26:14 +00001019 return &TyDIE;
David Blaikie2ea848b2013-11-19 22:51:04 +00001020}
1021
Eric Christophera5a79422013-12-09 23:32:48 +00001022void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1023 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001024 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1025 bool IsImplementation = 0;
1026 if (Ty.isCompositeType()) {
1027 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001028 // A runtime language of 0 actually means C/C++ and that any
1029 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001030 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001031 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001032 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
David Blaikie18d33752014-04-24 01:23:49 +00001033 DD->addAccelType(Ty.getName(), TyDIE, Flags);
David Blaikie9d861be2013-11-26 00:15:27 +00001034
David Blaikieadbea1e2014-03-12 03:34:38 +00001035 if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1036 Context.isNameSpace()) &&
1037 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
David Blaikie9d861be2013-11-26 00:15:27 +00001038 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001039 }
Devang Patel0e821f42011-04-12 23:21:44 +00001040}
1041
1042/// addType - Add a new type attribute to the specified entity.
David Blaikie65a74662014-04-25 18:26:14 +00001043void DwarfUnit::addType(DIE &Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001044 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001045
1046 // Check for pre-existence.
1047 DIEEntry *Entry = getDIEEntry(Ty);
1048 // If it exists then use the existing value.
1049 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001050 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001051 return;
1052 }
1053
1054 // Construct type.
1055 DIE *Buffer = getOrCreateTypeDIE(Ty);
1056
1057 // Set up proxy.
1058 Entry = createDIEEntry(Buffer);
1059 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001060 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001061}
1062
Eric Christopher9c58f312013-09-20 22:20:55 +00001063/// addGlobalName - Add a new global name to the compile unit.
David Blaikie65a74662014-04-25 18:26:14 +00001064void DwarfUnit::addGlobalName(StringRef Name, DIE &Die, DIScope Context) {
David Blaikieadbea1e2014-03-12 03:34:38 +00001065 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1066 return;
Eric Christopher8dba0d52013-10-19 01:04:42 +00001067 std::string FullName = getParentContextString(Context) + Name.str();
David Blaikie65a74662014-04-25 18:26:14 +00001068 GlobalNames[FullName] = &Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001069}
1070
Eric Christopher2c8b7902013-10-17 02:06:06 +00001071/// getParentContextString - Walks the metadata parent chain in a language
1072/// specific manner (using the compile unit language) and returns
1073/// it as a string. This is done at the metadata level because DIEs may
1074/// not currently have been added to the parent context and walking the
1075/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001076std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001077 if (!Context)
1078 return "";
1079
1080 // FIXME: Decide whether to implement this for non-C++ languages.
1081 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1082 return "";
1083
Eric Christopher8dba0d52013-10-19 01:04:42 +00001084 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001085 SmallVector<DIScope, 1> Parents;
1086 while (!Context.isCompileUnit()) {
1087 Parents.push_back(Context);
1088 if (Context.getContext())
1089 Context = resolve(Context.getContext());
1090 else
1091 // Structure, etc types will have a NULL context if they're at the top
1092 // level.
1093 break;
1094 }
1095
1096 // Reverse iterate over our list to go from the outermost construct to the
1097 // innermost.
1098 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1099 E = Parents.rend();
1100 I != E; ++I) {
1101 DIScope Ctx = *I;
1102 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001103 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001104 CS += Name;
1105 CS += "::";
1106 }
1107 }
1108 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001109}
1110
1111/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001112void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001113 // Get core information.
1114 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001115 // Add name if not anonymous or intermediate type.
1116 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001117 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001118
David Blaikiefac56122013-10-04 23:21:16 +00001119 // An unspecified type only has a name attribute.
1120 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001121 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001122
David Blaikie65a74662014-04-25 18:26:14 +00001123 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001124 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001125
Devang Patel0e821f42011-04-12 23:21:44 +00001126 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikie65a74662014-04-25 18:26:14 +00001127 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001128}
1129
1130/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001131void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001132 // Get core information.
1133 StringRef Name = DTy.getName();
1134 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001135 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001136
1137 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001138 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001139 if (FromTy)
David Blaikie65a74662014-04-25 18:26:14 +00001140 addType(Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001141
1142 // Add name if not anonymous or intermediate type.
1143 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001144 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001145
1146 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001147 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikie65a74662014-04-25 18:26:14 +00001148 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001149
David Blaikie5d3249b2013-01-07 05:51:15 +00001150 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
David Blaikie65a74662014-04-25 18:26:14 +00001151 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
Eric Christopherc2697f82013-10-19 01:04:47 +00001152 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001153 // Add source line info if available and TyDesc is not a forward declaration.
1154 if (!DTy.isForwardDecl())
David Blaikie65a74662014-04-25 18:26:14 +00001155 addSourceLine(Buffer, DTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001156}
1157
Adrian Prantl3f49c892014-02-25 19:57:42 +00001158/// constructSubprogramArguments - Construct function argument DIEs.
1159void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
Adrian Prantl69140d22014-02-25 22:27:14 +00001160 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1161 DIDescriptor Ty = Args.getElement(i);
1162 if (Ty.isUnspecifiedParameter()) {
1163 assert(i == N-1 && "Unspecified parameter must be the last argument");
1164 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1165 } else {
1166 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
David Blaikie65a74662014-04-25 18:26:14 +00001167 addType(*Arg, DIType(Ty));
Adrian Prantl69140d22014-02-25 22:27:14 +00001168 if (DIType(Ty).isArtificial())
David Blaikie65a74662014-04-25 18:26:14 +00001169 addFlag(*Arg, dwarf::DW_AT_artificial);
Adrian Prantl3f49c892014-02-25 19:57:42 +00001170 }
Adrian Prantl69140d22014-02-25 22:27:14 +00001171 }
Adrian Prantl3f49c892014-02-25 19:57:42 +00001172}
1173
Devang Patel0e821f42011-04-12 23:21:44 +00001174/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001175void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001176 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001177 StringRef Name = CTy.getName();
1178
1179 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001180 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001181
1182 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001183 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001184 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001185 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001186 case dwarf::DW_TAG_enumeration_type:
1187 constructEnumTypeDIE(Buffer, CTy);
1188 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001189 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001190 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001191 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001192 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001193 if (RTy)
David Blaikie65a74662014-04-25 18:26:14 +00001194 addType(Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001195
1196 bool isPrototyped = true;
Adrian Prantl3f49c892014-02-25 19:57:42 +00001197 if (Elements.getNumElements() == 2 &&
1198 Elements.getElement(1).isUnspecifiedParameter())
1199 isPrototyped = false;
1200
1201 constructSubprogramArguments(Buffer, Elements);
1202
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001203 // Add prototype flag if we're dealing with a C language and the
1204 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001205 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001206 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001207 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001208 Language == dwarf::DW_LANG_ObjC))
David Blaikie65a74662014-04-25 18:26:14 +00001209 addFlag(Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001210
1211 if (CTy.isLValueReference())
David Blaikie65a74662014-04-25 18:26:14 +00001212 addFlag(Buffer, dwarf::DW_AT_reference);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001213
1214 if (CTy.isRValueReference())
David Blaikie65a74662014-04-25 18:26:14 +00001215 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001216 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001217 case dwarf::DW_TAG_structure_type:
1218 case dwarf::DW_TAG_union_type:
1219 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001220 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001221 DIArray Elements = CTy.getTypeArray();
1222 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001223 DIDescriptor Element = Elements.getElement(i);
Adrian Prantlef129fb2014-01-18 02:12:00 +00001224 if (Element.isSubprogram())
David Blaikie65a74662014-04-25 18:26:14 +00001225 getOrCreateSubprogramDIE(DISubprogram(Element));
Adrian Prantlef129fb2014-01-18 02:12:00 +00001226 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001227 DIDerivedType DDTy(Element);
1228 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
David Blaikie65a74662014-04-25 18:26:14 +00001229 DIE &ElemDie = *createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001230 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001231 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001232 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001233 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001234 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001235 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001236 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001237 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001238 DIObjCProperty Property(Element);
David Blaikie65a74662014-04-25 18:26:14 +00001239 DIE &ElemDie = *createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001240 StringRef PropertyName = Property.getObjCPropertyName();
1241 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001242 if (Property.getType())
1243 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001244 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001245 StringRef GetterName = Property.getObjCPropertyGetterName();
1246 if (!GetterName.empty())
1247 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1248 StringRef SetterName = Property.getObjCPropertySetterName();
1249 if (!SetterName.empty())
1250 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1251 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001252 if (Property.isReadOnlyObjCProperty())
1253 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1254 if (Property.isReadWriteObjCProperty())
1255 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1256 if (Property.isAssignObjCProperty())
1257 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1258 if (Property.isRetainObjCProperty())
1259 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1260 if (Property.isCopyObjCProperty())
1261 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1262 if (Property.isNonAtomicObjCProperty())
1263 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1264 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001265 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001266 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001267
Devang Pateld925d1a2012-02-07 23:33:58 +00001268 DIEEntry *Entry = getDIEEntry(Element);
1269 if (!Entry) {
David Blaikie65a74662014-04-25 18:26:14 +00001270 Entry = createDIEEntry(&ElemDie);
Devang Pateld925d1a2012-02-07 23:33:58 +00001271 insertDIEEntry(Element, Entry);
1272 }
Devang Patel403e8192012-02-04 01:30:32 +00001273 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001274 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001275 }
1276
1277 if (CTy.isAppleBlockExtension())
David Blaikie65a74662014-04-25 18:26:14 +00001278 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001279
Eric Christopher9e429ae2013-10-05 00:27:02 +00001280 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001281 if (ContainingType)
David Blaikie65a74662014-04-25 18:26:14 +00001282 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001283 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001284
Devang Patel12419ae2011-05-12 21:29:42 +00001285 if (CTy.isObjcClassComplete())
David Blaikie65a74662014-04-25 18:26:14 +00001286 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001287
Eric Christopherda011dd2011-12-16 23:42:42 +00001288 // Add template parameters to a class, structure or union types.
1289 // FIXME: The support isn't in the metadata for this yet.
1290 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001291 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001292 addTemplateParams(Buffer, CTy.getTemplateParams());
1293
1294 break;
1295 }
1296 default:
1297 break;
1298 }
1299
1300 // Add name if not anonymous or intermediate type.
1301 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001302 addString(Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001303
Eric Christopher775cbd22012-05-22 18:45:18 +00001304 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001305 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001306 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001307 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001308 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001309 if (Size)
David Blaikie65a74662014-04-25 18:26:14 +00001310 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001311 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001312 // Add zero size if it is not a forward declaration.
David Blaikie65a74662014-04-25 18:26:14 +00001313 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001314
1315 // If we're a forward decl, say so.
1316 if (CTy.isForwardDecl())
David Blaikie65a74662014-04-25 18:26:14 +00001317 addFlag(Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001318
1319 // Add source line info if available.
1320 if (!CTy.isForwardDecl())
David Blaikie65a74662014-04-25 18:26:14 +00001321 addSourceLine(Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001322
1323 // No harm in adding the runtime language to the declaration.
1324 unsigned RLang = CTy.getRunTimeLang();
1325 if (RLang)
David Blaikie65a74662014-04-25 18:26:14 +00001326 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001327 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001328 }
1329}
1330
Manman Renffc9a712013-10-23 23:05:28 +00001331/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1332/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001333void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1334 DITemplateTypeParameter TP) {
David Blaikie65a74662014-04-25 18:26:14 +00001335 DIE &ParamDIE =
1336 *createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001337 // Add the type if it exists, it could be void and therefore no type.
1338 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001339 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001340 if (!TP.getName().empty())
1341 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001342}
1343
Manman Renffc9a712013-10-23 23:05:28 +00001344/// constructTemplateValueParameterDIE - Construct new DIE for the given
1345/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001346void
1347DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001348 DITemplateValueParameter VP) {
David Blaikie65a74662014-04-25 18:26:14 +00001349 DIE &ParamDIE = *createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001350
1351 // Add the type if there is one, template template and template parameter
1352 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001353 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001354 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001355 if (!VP.getName().empty())
1356 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1357 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001358 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001359 addConstantValue(ParamDIE, CI,
1360 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001361 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1362 // For declaration non-type template parameters (such as global values and
1363 // functions)
Eric Christopher4a741042014-02-16 08:46:55 +00001364 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001365 addOpAddress(*Loc, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001366 // Emit DW_OP_stack_value to use the address as the immediate value of the
1367 // parameter, rather than a pointer to it.
David Blaikie65a74662014-04-25 18:26:14 +00001368 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
Eric Christopher4a741042014-02-16 08:46:55 +00001369 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafb2c412013-08-08 07:40:31 +00001370 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001371 assert(isa<MDString>(Val));
1372 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1373 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001374 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001375 assert(isa<MDNode>(Val));
1376 DIArray A(cast<MDNode>(Val));
David Blaikie65a74662014-04-25 18:26:14 +00001377 addTemplateParams(ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001378 }
1379 }
Devang Patel0e821f42011-04-12 23:21:44 +00001380}
1381
Devang Patel17b53272011-05-06 16:57:54 +00001382/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001383DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001384 // Construct the context before querying for the existence of the DIE in case
1385 // such construction creates the DIE.
1386 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001387
David Blaikie65a74662014-04-25 18:26:14 +00001388 if (DIE *NDie = getDIE(NS))
Devang Patel17b53272011-05-06 16:57:54 +00001389 return NDie;
David Blaikie65a74662014-04-25 18:26:14 +00001390 DIE &NDie = *createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
Manman Renf6b936b2013-10-29 05:49:41 +00001391
Eric Christopher4996c702011-11-07 09:24:32 +00001392 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001393 addString(NDie, dwarf::DW_AT_name, NS.getName());
David Blaikie65a74662014-04-25 18:26:14 +00001394 DD->addAccelNamespace(NS.getName(), &NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001395 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001396 } else
David Blaikie65a74662014-04-25 18:26:14 +00001397 DD->addAccelNamespace("(anonymous namespace)", &NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001398 addSourceLine(NDie, NS);
David Blaikie65a74662014-04-25 18:26:14 +00001399 return &NDie;
Devang Patel17b53272011-05-06 16:57:54 +00001400}
1401
Devang Patel89543712011-08-15 17:24:54 +00001402/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001403DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001404 // Construct the context before querying for the existence of the DIE in case
1405 // such construction creates the DIE (as is the case for member function
1406 // declarations).
Adrian Prantld1e6a4e2014-03-14 23:08:25 +00001407 DIScope Context = resolve(SP.getContext());
1408 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantld486b342014-03-18 17:41:15 +00001409
Adrian Prantld1e6a4e2014-03-14 23:08:25 +00001410 // Unique declarations based on the ODR, where applicable.
Adrian Prantld486b342014-03-18 17:41:15 +00001411 SP = DISubprogram(DD->resolve(SP.getRef()));
1412 assert(SP.Verify());
David Blaikie309ffe42013-10-04 01:39:59 +00001413
David Blaikie65a74662014-04-25 18:26:14 +00001414 if (DIE *SPDie = getDIE(SP))
Devang Patel89543712011-08-15 17:24:54 +00001415 return SPDie;
1416
Manman Ren73d697c2013-10-29 00:58:04 +00001417 DISubprogram SPDecl = SP.getFunctionDeclaration();
1418 if (SPDecl.isSubprogram())
1419 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001420 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001421
1422 // DW_TAG_inlined_subroutine may refer to this DIE.
David Blaikie65a74662014-04-25 18:26:14 +00001423 DIE &SPDie = *createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001424
Craig Topper353eda42014-04-24 06:44:33 +00001425 DIE *DeclDie = nullptr;
Manman Renb9512a72013-10-23 22:12:26 +00001426 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001427 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001428
Devang Patel89543712011-08-15 17:24:54 +00001429 // Add function template parameters.
David Blaikie65a74662014-04-25 18:26:14 +00001430 addTemplateParams(SPDie, SP.getTemplateParams());
Devang Patel89543712011-08-15 17:24:54 +00001431
Adrian Prantl8714aaf2014-04-14 21:16:04 +00001432 if (DeclDie)
Rafael Espindola79278362011-11-10 22:34:29 +00001433 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001434 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001435
Adrian Prantl8714aaf2014-04-14 21:16:04 +00001436 // Add the linkage name if we have one and it isn't in the Decl.
1437 StringRef LinkageName = SP.getLinkageName();
1438 if (!LinkageName.empty()) {
1439 if (SPDecl.isSubprogram() && !SPDecl.getLinkageName().empty())
1440 assert(SPDecl.getLinkageName() == SP.getLinkageName() &&
1441 "decl has a linkage name and it is different");
1442 else
1443 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1444 GlobalValue::getRealLinkageName(LinkageName));
Rafael Espindola79278362011-11-10 22:34:29 +00001445 }
Devang Patel89543712011-08-15 17:24:54 +00001446
Adrian Prantl8714aaf2014-04-14 21:16:04 +00001447 // If this DIE is going to refer declaration info using AT_specification
1448 // then there is no need to add other attributes.
1449 if (DeclDie)
David Blaikie65a74662014-04-25 18:26:14 +00001450 return &SPDie;
Eric Christopheracb71152012-08-23 22:52:55 +00001451
Devang Patel89543712011-08-15 17:24:54 +00001452 // Constructors and operators for anonymous aggregates do not have names.
1453 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001454 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001455
1456 addSourceLine(SPDie, SP);
1457
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001458 // Add the prototype if we have a prototype and we have a C like
1459 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001460 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001461 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001462 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001463 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001464 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001465
Devang Patel89543712011-08-15 17:24:54 +00001466 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001467 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1468 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001469
David Blaikie5174c842013-05-22 23:22:18 +00001470 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001471 // Add a return type. If this is a type like a C/C++ void type we don't add a
1472 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001473 if (Args.getElement(0))
1474 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001475
1476 unsigned VK = SP.getVirtuality();
1477 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001478 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Eric Christopher4a741042014-02-16 08:46:55 +00001479 DIELoc *Block = getDIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001480 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1481 addUInt(*Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
David Blaikief2443192013-10-21 17:28:37 +00001482 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001483 ContainingTypeMap.insert(
David Blaikie65a74662014-04-25 18:26:14 +00001484 std::make_pair(&SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001485 }
1486
1487 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001488 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001489
Devang Patel89543712011-08-15 17:24:54 +00001490 // Add arguments. Do not add arguments for subprogram definition. They will
1491 // be handled while processing variables.
David Blaikie65a74662014-04-25 18:26:14 +00001492 constructSubprogramArguments(SPDie, Args);
Devang Patel89543712011-08-15 17:24:54 +00001493 }
1494
1495 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001496 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001497
1498 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001499 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001500
1501 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001502 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001503
1504 if (unsigned isa = Asm->getISAEncoding()) {
1505 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1506 }
1507
Adrian Prantl99c7af22013-12-18 21:48:19 +00001508 if (SP.isLValueReference())
1509 addFlag(SPDie, dwarf::DW_AT_reference);
1510
1511 if (SP.isRValueReference())
1512 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1513
Adrian Prantlef129fb2014-01-18 02:12:00 +00001514 if (SP.isProtected())
1515 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1516 dwarf::DW_ACCESS_protected);
1517 else if (SP.isPrivate())
1518 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1519 dwarf::DW_ACCESS_private);
1520 else
1521 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1522 dwarf::DW_ACCESS_public);
1523
1524 if (SP.isExplicit())
1525 addFlag(SPDie, dwarf::DW_AT_explicit);
1526
David Blaikie65a74662014-04-25 18:26:14 +00001527 return &SPDie;
Devang Patel89543712011-08-15 17:24:54 +00001528}
1529
Devang Pateldfd6ec32011-08-15 17:57:41 +00001530// Return const expression if value is a GEP to access merged global
1531// constant. e.g.
1532// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1533static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1534 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1535 if (!CE || CE->getNumOperands() != 3 ||
1536 CE->getOpcode() != Instruction::GetElementPtr)
Craig Topper353eda42014-04-24 06:44:33 +00001537 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001538
1539 // First operand points to a global struct.
1540 Value *Ptr = CE->getOperand(0);
1541 if (!isa<GlobalValue>(Ptr) ||
1542 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
Craig Topper353eda42014-04-24 06:44:33 +00001543 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001544
1545 // Second operand is zero.
1546 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1547 if (!CI || !CI->isZero())
Craig Topper353eda42014-04-24 06:44:33 +00001548 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001549
1550 // Third operand is offset.
1551 if (!isa<ConstantInt>(CE->getOperand(2)))
Craig Topper353eda42014-04-24 06:44:33 +00001552 return nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001553
1554 return CE;
1555}
1556
1557/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001558void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001559 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001560 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001561 return;
1562
David Blaikie5e390e42014-02-04 01:23:52 +00001563 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001564
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001565 DIScope GVContext = GV.getContext();
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001566 DIType GTy = DD->resolve(GV.getType());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001567
1568 // If this is a static data member definition, some attributes belong
1569 // to the declaration DIE.
Craig Topper353eda42014-04-24 06:44:33 +00001570 DIE *VariableDIE = nullptr;
Manman Rene697d3c2013-02-01 23:54:37 +00001571 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001572 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1573 if (SDMDecl.Verify()) {
1574 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1575 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001576 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001577 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001578 }
1579
1580 // If this is not a static data member definition, create the variable
1581 // DIE and add the initial set of attributes to it.
1582 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001583 // Construct the context before querying for the existence of the DIE in
1584 // case such construction creates the DIE.
1585 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001586
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001587 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001588 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001589
1590 // Add name and type.
David Blaikie65a74662014-04-25 18:26:14 +00001591 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1592 addType(*VariableDIE, GTy);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001593
1594 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001595 if (!GV.isLocalToUnit())
David Blaikie65a74662014-04-25 18:26:14 +00001596 addFlag(*VariableDIE, dwarf::DW_AT_external);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001597
1598 // Add line number info.
David Blaikie65a74662014-04-25 18:26:14 +00001599 addSourceLine(*VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001600 }
1601
Devang Pateldfd6ec32011-08-15 17:57:41 +00001602 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001603 bool addToAccelTable = false;
Craig Topper353eda42014-04-24 06:44:33 +00001604 DIE *VariableSpecDIE = nullptr;
1605 bool isGlobalVariable = GV.getGlobal() != nullptr;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001606 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001607 addToAccelTable = true;
Eric Christopher4a741042014-02-16 08:46:55 +00001608 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001609 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001610 if (GV.getGlobal()->isThreadLocal()) {
1611 // FIXME: Make this work with -gsplit-dwarf.
1612 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1613 assert((PointerSize == 4 || PointerSize == 8) &&
1614 "Add support for other sizes if necessary");
1615 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001616 if (!DD->useSplitDwarf()) {
1617 // 1) Start with a constNu of the appropriate pointer size
David Blaikie65a74662014-04-25 18:26:14 +00001618 addUInt(*Loc, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001619 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001620 // 2) containing the (relocated) offset of the TLS variable
1621 // within the module's TLS block.
David Blaikie65a74662014-04-25 18:26:14 +00001622 addExpr(*Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001623 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001624 } else {
David Blaikie65a74662014-04-25 18:26:14 +00001625 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1626 addUInt(*Loc, dwarf::DW_FORM_udata,
David Blaikied75fb282014-04-23 21:20:10 +00001627 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001628 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001629 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikie65a74662014-04-25 18:26:14 +00001630 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001631 } else {
1632 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikie65a74662014-04-25 18:26:14 +00001633 addOpAddress(*Loc, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001634 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001635 // Do not create specification DIE if context is either compile unit
1636 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001637 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001638 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001639 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001640 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
David Blaikie65a74662014-04-25 18:26:14 +00001641 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
1642 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001643 // A static member's declaration is already flagged as such.
1644 if (!SDMDecl.Verify())
David Blaikie65a74662014-04-25 18:26:14 +00001645 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001646 } else {
David Blaikie65a74662014-04-25 18:26:14 +00001647 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4996c702011-11-07 09:24:32 +00001648 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001649 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001650 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001651 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001652 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1653 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1654 // TAG_variable.
David Blaikie65a74662014-04-25 18:26:14 +00001655 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE
1656 : *VariableDIE,
Eric Christopheraf7eca22014-03-13 23:26:25 +00001657 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1658 : dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001659 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001660 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001661 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001662 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001663 // emitting AT_const_value multiple times, we only add AT_const_value when
1664 // it is not a static member.
1665 if (!IsStaticMember)
David Blaikie65a74662014-04-25 18:26:14 +00001666 addConstantValue(*VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001667 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001668 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001669 // GV is a merged global.
Eric Christopher4a741042014-02-16 08:46:55 +00001670 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Pateldfd6ec32011-08-15 17:57:41 +00001671 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001672 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1673 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikie65a74662014-04-25 18:26:14 +00001674 addOpAddress(*Loc, Sym);
1675 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001676 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikie65a74662014-04-25 18:26:14 +00001677 addUInt(*Loc, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001678 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikie65a74662014-04-25 18:26:14 +00001679 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1680 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001681 }
1682
Eric Christopherc12c2112011-11-11 01:55:22 +00001683 if (addToAccelTable) {
1684 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
David Blaikie2406a0622014-04-23 23:37:35 +00001685 DD->addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001686
Eric Christopherc12c2112011-11-11 01:55:22 +00001687 // If the linkage name is different than the name, go ahead and output
1688 // that as well into the name table.
1689 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
David Blaikie2406a0622014-04-23 23:37:35 +00001690 DD->addAccelName(GV.getLinkageName(), AddrDIE);
Eric Christopherc12c2112011-11-11 01:55:22 +00001691 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001692
1693 if (!GV.isLocalToUnit())
David Blaikie65a74662014-04-25 18:26:14 +00001694 addGlobalName(GV.getName(),
1695 VariableSpecDIE ? *VariableSpecDIE : *VariableDIE,
Eric Christopher2c8b7902013-10-17 02:06:06 +00001696 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001697}
1698
Devang Patel0e821f42011-04-12 23:21:44 +00001699/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001700void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
David Blaikie65a74662014-04-25 18:26:14 +00001701 DIE &DW_Subrange = *createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001702 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001703
Bill Wendling28fe9e72012-12-06 07:38:10 +00001704 // The LowerBound value defines the lower bounds which is typically zero for
1705 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1706 // Count == -1 then the array is unbounded and we do not emit
1707 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1708 // Count == 0, then the array has zero elements in which case we do not emit
1709 // an upper bound.
1710 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001711 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001712 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001713
Bill Wendling3495f9b2012-12-06 07:55:19 +00001714 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001715 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001716
1717 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001718 // FIXME: An unbounded array should reference the expression that defines
1719 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001720 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1721 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001722}
1723
1724/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001725void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001726 if (CTy.isVector())
David Blaikie65a74662014-04-25 18:26:14 +00001727 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001728
Eric Christopher0df08e22013-08-08 07:40:37 +00001729 // Emit the element type.
David Blaikie65a74662014-04-25 18:26:14 +00001730 addType(Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001731
1732 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001733 // FIXME: This type should be passed down from the front end
1734 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001735 DIE *IdxTy = getIndexTyDie();
1736 if (!IdxTy) {
David Blaikie12e00fc2014-04-03 06:28:20 +00001737 // Construct an integer type to use for indexes.
David Blaikie2a80e442013-12-02 22:09:48 +00001738 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
David Blaikie65a74662014-04-25 18:26:14 +00001739 addString(*IdxTy, dwarf::DW_AT_name, "sizetype");
1740 addUInt(*IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1741 addUInt(*IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
David Blaikie12e00fc2014-04-03 06:28:20 +00001742 dwarf::DW_ATE_unsigned);
Devang Patel0e821f42011-04-12 23:21:44 +00001743 setIndexTyDie(IdxTy);
1744 }
1745
1746 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001747 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001748 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1749 DIDescriptor Element = Elements.getElement(i);
1750 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1751 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1752 }
1753}
1754
Eric Christopheraeb105f2013-11-11 18:52:39 +00001755/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001756void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001757 DIArray Elements = CTy.getTypeArray();
1758
1759 // Add enumerators to enumeration type.
1760 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001761 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001762 if (Enum.isEnumerator()) {
David Blaikie65a74662014-04-25 18:26:14 +00001763 DIE &Enumerator = *createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001764 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001765 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001766 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001767 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1768 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001769 }
1770 }
1771 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1772 if (DTy) {
David Blaikie65a74662014-04-25 18:26:14 +00001773 addType(Buffer, DTy);
1774 addFlag(Buffer, dwarf::DW_AT_enum_class);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001775 }
Devang Patel0e821f42011-04-12 23:21:44 +00001776}
1777
Devang Patel89543712011-08-15 17:24:54 +00001778/// constructContainingTypeDIEs - Construct DIEs for types that contain
1779/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001780void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001781 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001782 CE = ContainingTypeMap.end();
1783 CI != CE; ++CI) {
David Blaikie65a74662014-04-25 18:26:14 +00001784 DIE &SPDie = *CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001785 DIDescriptor D(CI->second);
1786 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001787 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001788 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001789 if (!NDie)
1790 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001791 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001792 }
1793}
1794
Devang Patel3acc70e2011-08-15 22:04:40 +00001795/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001796DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
David Blaikiee071fc82014-04-25 17:32:19 +00001797 auto D = constructVariableDIEImpl(DV, isScopeAbstract);
1798 DV.setDIE(*D);
1799 return D;
1800}
1801
1802DIE *DwarfUnit::constructVariableDIEImpl(const DbgVariable &DV,
1803 bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001804 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001805
Devang Patel3acc70e2011-08-15 22:04:40 +00001806 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001807 DIE *VariableDie = new DIE(DV.getTag());
1808 DbgVariable *AbsVar = DV.getAbstractVariable();
Craig Topper353eda42014-04-24 06:44:33 +00001809 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : nullptr;
Manman Ren4213c392013-05-29 17:16:59 +00001810 if (AbsDIE)
David Blaikie65a74662014-04-25 18:26:14 +00001811 addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001812 else {
David Blaikie715528b2013-08-19 03:34:03 +00001813 if (!Name.empty())
David Blaikie65a74662014-04-25 18:26:14 +00001814 addString(*VariableDie, dwarf::DW_AT_name, Name);
1815 addSourceLine(*VariableDie, DV.getVariable());
1816 addType(*VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001817 }
1818
Eric Christopher34a2c872013-11-15 01:43:19 +00001819 if (DV.isArtificial())
David Blaikie65a74662014-04-25 18:26:14 +00001820 addFlag(*VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001821
David Blaikiee071fc82014-04-25 17:32:19 +00001822 if (isScopeAbstract)
Devang Patel3acc70e2011-08-15 22:04:40 +00001823 return VariableDie;
Devang Patel3acc70e2011-08-15 22:04:40 +00001824
1825 // Add variable address.
1826
Eric Christopher34a2c872013-11-15 01:43:19 +00001827 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001828 if (Offset != ~0U) {
David Blaikie65a74662014-04-25 18:26:14 +00001829 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
Devang Patel3acc70e2011-08-15 22:04:40 +00001830 return VariableDie;
1831 }
1832
Eric Christophercead0332011-10-03 15:49:20 +00001833 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001834 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001835 assert(DVInsn->getNumOperands() == 3);
1836 if (DVInsn->getOperand(0).isReg()) {
1837 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001838 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001839 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001840 MachineLocation Location(RegOp.getReg(),
1841 DVInsn->getOperand(1).getImm());
David Blaikie65a74662014-04-25 18:26:14 +00001842 addVariableAddress(DV, *VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001843 } else if (RegOp.getReg())
David Blaikie65a74662014-04-25 18:26:14 +00001844 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001845 } else if (DVInsn->getOperand(0).isImm())
David Blaikie65a74662014-04-25 18:26:14 +00001846 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001847 else if (DVInsn->getOperand(0).isFPImm())
David Blaikie65a74662014-04-25 18:26:14 +00001848 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001849 else if (DVInsn->getOperand(0).isCImm())
David Blaikie65a74662014-04-25 18:26:14 +00001850 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001851 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001852
Devang Patel3acc70e2011-08-15 22:04:40 +00001853 return VariableDie;
Devang Patel3acc70e2011-08-15 22:04:40 +00001854 }
1855
David Blaikiee071fc82014-04-25 17:32:19 +00001856 // .. else use frame index.
1857 int FI = DV.getFrameIndex();
1858 if (FI != ~0) {
1859 unsigned FrameReg = 0;
1860 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1861 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1862 MachineLocation Location(FrameReg, Offset);
David Blaikie65a74662014-04-25 18:26:14 +00001863 addVariableAddress(DV, *VariableDie, Location);
David Blaikiee071fc82014-04-25 17:32:19 +00001864 }
1865
Devang Patel3acc70e2011-08-15 22:04:40 +00001866 return VariableDie;
1867}
1868
Manman Ren230ec862013-10-23 23:00:44 +00001869/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001870void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
David Blaikie65a74662014-04-25 18:26:14 +00001871 DIE &MemberDie = *createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001872 StringRef Name = DT.getName();
1873 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001874 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001875
Manman Ren93b30902013-10-08 18:42:58 +00001876 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001877
1878 addSourceLine(MemberDie, DT);
1879
Eric Christopherc2697f82013-10-19 01:04:47 +00001880 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001881
1882 // For C++, virtual base classes are not at fixed offset. Use following
1883 // expression to extract appropriate offset from vtable.
1884 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1885
Eric Christopher4a741042014-02-16 08:46:55 +00001886 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001887 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1888 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1889 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1890 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1891 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1892 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1893 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001894
David Blaikief2443192013-10-21 17:28:37 +00001895 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001896 } else {
1897 uint64_t Size = DT.getSizeInBits();
1898 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1899 uint64_t OffsetInBytes;
1900
1901 if (Size != FieldSize) {
Eric Christopher1acdbb82014-03-12 17:14:46 +00001902 // Handle bitfield, assume bytes are 8 bits.
1903 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1904 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
David Blaikie71d34a22013-11-01 00:25:45 +00001905
1906 uint64_t Offset = DT.getOffsetInBits();
1907 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1908 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1909 uint64_t FieldOffset = (HiMark - FieldSize);
1910 Offset -= FieldOffset;
1911
1912 // Maybe we need to work from the other end.
1913 if (Asm->getDataLayout().isLittleEndian())
1914 Offset = FieldSize - (Offset + Size);
1915 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1916
Adrian Prantlc67655a2014-01-28 18:13:47 +00001917 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001918 // field that includes this bit field.
1919 OffsetInBytes = FieldOffset >> 3;
1920 } else
1921 // This is not a bitfield.
1922 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001923
David Blaikie22b29a52014-01-03 01:30:05 +00001924 if (DD->getDwarfVersion() <= 2) {
Eric Christopher4a741042014-02-16 08:46:55 +00001925 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie65a74662014-04-25 18:26:14 +00001926 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1927 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
David Blaikie22b29a52014-01-03 01:30:05 +00001928 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1929 } else
1930 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1931 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001932 }
Devang Patel0e821f42011-04-12 23:21:44 +00001933
1934 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001935 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001936 dwarf::DW_ACCESS_protected);
1937 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001938 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001939 dwarf::DW_ACCESS_private);
1940 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001941 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001942 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001943 dwarf::DW_ACCESS_public);
1944 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001945 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001946 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001947
1948 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001949 if (MDNode *PNode = DT.getObjCProperty())
1950 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
David Blaikie65a74662014-04-25 18:26:14 +00001951 MemberDie.addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1952 PropertyDie);
Devang Patel44882172012-02-06 17:49:43 +00001953
David Blaikie37fefc32012-12-13 22:43:07 +00001954 if (DT.isArtificial())
1955 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001956}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001957
Manman Renc6b63922013-10-14 20:33:57 +00001958/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001959DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001960 if (!DT.Verify())
Craig Topper353eda42014-04-24 06:44:33 +00001961 return nullptr;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001962
Manman Renc6b63922013-10-14 20:33:57 +00001963 // Construct the context before querying for the existence of the DIE in case
1964 // such construction creates the DIE.
1965 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001966 assert(dwarf::isType(ContextDIE->getTag()) &&
1967 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001968
David Blaikie65a74662014-04-25 18:26:14 +00001969 if (DIE *StaticMemberDIE = getDIE(DT))
Manman Renc6b63922013-10-14 20:33:57 +00001970 return StaticMemberDIE;
1971
David Blaikie65a74662014-04-25 18:26:14 +00001972 DIE &StaticMemberDIE = *createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001973
Manman Ren93b30902013-10-08 18:42:58 +00001974 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001975
1976 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1977 addType(StaticMemberDIE, Ty);
1978 addSourceLine(StaticMemberDIE, DT);
1979 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1980 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1981
1982 // FIXME: We could omit private if the parent is a class_type, and
1983 // public if the parent is something else.
1984 if (DT.isProtected())
1985 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1986 dwarf::DW_ACCESS_protected);
1987 else if (DT.isPrivate())
1988 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1989 dwarf::DW_ACCESS_private);
1990 else
1991 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1992 dwarf::DW_ACCESS_public);
1993
1994 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001995 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001996 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1997 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001998
David Blaikie65a74662014-04-25 18:26:14 +00001999 return &StaticMemberDIE;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002000}
David Blaikie6b288cf2013-10-30 20:42:41 +00002001
David Blaikied82b2372014-03-24 20:28:10 +00002002void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00002003 Asm->OutStreamer.AddComment("DWARF version number");
2004 Asm->EmitInt16(DD->getDwarfVersion());
2005 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00002006 // We share one abbreviations table across all units so it's always at the
2007 // start of the section. Use a relocatable offset where needed to ensure
2008 // linking doesn't invalidate that offset.
David Blaikie3c9a3cc2014-03-24 20:53:02 +00002009 if (ASectionSym)
2010 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
2011 else
David Blaikie326e1fa2014-04-02 02:04:51 +00002012 // Use a constant value when no symbol is provided.
David Blaikie3c9a3cc2014-03-24 20:53:02 +00002013 Asm->EmitInt32(0);
David Blaikie6b288cf2013-10-30 20:42:41 +00002014 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2015 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2016}
David Blaikiebc563272013-12-13 21:33:40 +00002017
Eric Christopher384f3fe2014-03-20 19:16:16 +00002018void DwarfUnit::addRange(RangeSpan Range) {
2019 // Only add a range for this unit if we're emitting full debug.
2020 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) {
2021 // If we have no current ranges just add the range and return, otherwise,
2022 // check the current section and CU against the previous section and CU we
2023 // emitted into and the subprogram was contained within. If these are the
2024 // same then extend our current range, otherwise add this as a new range.
2025 if (CURanges.size() == 0 ||
2026 this != DD->getPrevCU() ||
2027 Asm->getCurrentSection() != DD->getPrevSection()) {
2028 CURanges.push_back(Range);
2029 return;
2030 }
2031
2032 assert(&(CURanges.back().getEnd()->getSection()) ==
2033 &(Range.getEnd()->getSection()) &&
2034 "We can only append to a range in the same section!");
2035 CURanges.back().setEnd(Range.getEnd());
2036 }
2037}
2038
David Blaikie2494fdb2014-02-14 22:41:51 +00002039void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2040 // Define start line table label for each Compile Unit.
2041 MCSymbol *LineTableStartSym =
David Blaikie34641612014-04-01 08:07:52 +00002042 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
David Blaikie2494fdb2014-02-14 22:41:51 +00002043
David Blaikie60e63862014-02-14 23:58:13 +00002044 stmtListIndex = UnitDie->getValues().size();
2045
David Blaikie2494fdb2014-02-14 22:41:51 +00002046 // DW_AT_stmt_list is a offset of line number information for this
2047 // compile unit in debug_line section. For split dwarf this is
2048 // left in the skeleton CU and so not included.
2049 // The line table entries are not always emitted in assembly, so it
2050 // is not okay to use line_table_start here.
2051 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
David Blaikie65a74662014-04-25 18:26:14 +00002052 addSectionLabel(*UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym);
David Blaikie2494fdb2014-02-14 22:41:51 +00002053 else
David Blaikie65a74662014-04-25 18:26:14 +00002054 addSectionDelta(*UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
David Blaikie2494fdb2014-02-14 22:41:51 +00002055 DwarfLineSectionSym);
2056}
2057
David Blaikie60e63862014-02-14 23:58:13 +00002058void DwarfCompileUnit::applyStmtList(DIE &D) {
2059 D.addValue(dwarf::DW_AT_stmt_list,
2060 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2061 UnitDie->getValues()[stmtListIndex]);
2062}
2063
David Blaikied82b2372014-03-24 20:28:10 +00002064void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const {
2065 DwarfUnit::emitHeader(ASectionSym);
David Blaikiebc563272013-12-13 21:33:40 +00002066 Asm->OutStreamer.AddComment("Type Signature");
2067 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2068 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002069 // In a skeleton type unit there is no type DIE so emit a zero offset.
2070 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2071 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002072}
2073
2074void DwarfTypeUnit::initSection(const MCSection *Section) {
2075 assert(!this->Section);
2076 this->Section = Section;
2077 // Since each type unit is contained in its own COMDAT section, the begin
2078 // label and the section label are the same. Using the begin label emission in
2079 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2080 // the only other alternative of lazily constructing start-of-section labels
2081 // and storing a mapping in DwarfDebug (or AsmPrinter).
2082 this->SectionSym = this->LabelBegin =
2083 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2084 this->LabelEnd =
2085 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2086 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2087}