blob: 18070462b4a42e2983f9cd6a7718c97c9394a44e [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel0e821f42011-04-12 23:21:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher160522c2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel0e821f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
David Blaikie2c86a722013-12-02 19:33:15 +000016#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000026#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000027#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49}
50
Eric Christopher4287a492013-12-09 23:57:44 +000051DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000054 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000056}
57
David Blaikie15632ae2014-02-12 00:31:30 +000058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
Eric Christopher4287a492013-12-09 23:57:44 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
David Blaikie15632ae2014-02-12 00:31:30 +000060 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU) {
David Blaikie15632ae2014-02-12 00:31:30 +000061}
David Blaikie409dd9c2013-11-19 23:08:21 +000062
David Blaikie319a05f2013-12-02 19:33:10 +000063/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000064DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000065 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
66 DIEBlocks[j]->~DIEBlock();
67}
68
69/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
70/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000071DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000072 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
73 return Value;
74}
75
Bill Wendling3495f9b2012-12-06 07:55:19 +000076/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000077/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000078int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000079 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000080 default:
81 break;
82
83 case dwarf::DW_LANG_C89:
84 case dwarf::DW_LANG_C99:
85 case dwarf::DW_LANG_C:
86 case dwarf::DW_LANG_C_plus_plus:
87 case dwarf::DW_LANG_ObjC:
88 case dwarf::DW_LANG_ObjC_plus_plus:
89 return 0;
90
91 case dwarf::DW_LANG_Fortran77:
92 case dwarf::DW_LANG_Fortran90:
93 case dwarf::DW_LANG_Fortran95:
94 return 1;
95
96 // The languages below have valid values only if the DWARF version >= 4.
97 case dwarf::DW_LANG_Java:
98 case dwarf::DW_LANG_Python:
99 case dwarf::DW_LANG_UPC:
100 case dwarf::DW_LANG_D:
101 if (dwarf::DWARF_VERSION >= 4)
102 return 0;
103 break;
104
105 case dwarf::DW_LANG_Ada83:
106 case dwarf::DW_LANG_Ada95:
107 case dwarf::DW_LANG_Cobol74:
108 case dwarf::DW_LANG_Cobol85:
109 case dwarf::DW_LANG_Modula2:
110 case dwarf::DW_LANG_Pascal83:
111 case dwarf::DW_LANG_PLI:
112 if (dwarf::DWARF_VERSION >= 4)
113 return 1;
114 break;
115 }
116
117 return -1;
118}
119
Manman Ren4dbdc902013-10-31 17:54:35 +0000120/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000121static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000122 // When the MDNode can be part of the type system, the DIE can be shared
123 // across CUs.
124 // Combining type units and cross-CU DIE sharing is lower value (since
125 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
126 // level already) but may be implementable for some value in projects
127 // building multiple independent libraries with LTO and then linking those
128 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000129 return (D.isType() ||
130 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000131 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000132}
133
134/// getDIE - Returns the debug information entry map slot for the
135/// specified debug variable. We delegate the request to DwarfDebug
136/// when the DIE for this MDNode can be shared across CUs. The mappings
137/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000138DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000139 if (isShareableAcrossCUs(D))
140 return DD->getDIE(D);
141 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000142}
143
144/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
145/// when the DIE for this MDNode can be shared across CUs. The mappings
146/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000147void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000148 if (isShareableAcrossCUs(Desc)) {
149 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000150 return;
151 }
David Blaikie2ad00162013-11-15 23:09:13 +0000152 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000153}
154
Eric Christopherbb69a272012-08-24 01:14:27 +0000155/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000156void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000157 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000158 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000159 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000160 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000161}
162
Devang Patel0e821f42011-04-12 23:21:44 +0000163/// addUInt - Add an unsigned integer attribute data and value.
164///
Eric Christophera5a79422013-12-09 23:32:48 +0000165void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
166 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000167 if (!Form)
168 Form = DIEInteger::BestForm(false, Integer);
169 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
170 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000171 Die->addValue(Attribute, *Form, Value);
172}
173
Eric Christophera5a79422013-12-09 23:32:48 +0000174void DwarfUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000175 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000176}
177
178/// addSInt - Add an signed integer attribute data and value.
179///
Eric Christophera5a79422013-12-09 23:32:48 +0000180void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
181 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000182 if (!Form)
183 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000184 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000185 Die->addValue(Attribute, *Form, Value);
186}
187
Eric Christophera5a79422013-12-09 23:32:48 +0000188void DwarfUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
189 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000190 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000191}
192
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000193/// addString - Add a string attribute data and value. We always emit a
194/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000195/// more predictable sizes. In the case of split dwarf we emit an index
196/// into another table which gets us the static offset into the string
197/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000198void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
199 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000200
201 if (!DD->useSplitDwarf())
202 return addLocalString(Die, Attribute, String);
203
204 unsigned idx = DU->getStringPoolIndex(String);
205 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000206 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000207 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000208}
209
210/// addLocalString - Add a string attribute data and value. This is guaranteed
211/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000212void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
213 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000214 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000215 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000216 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000217 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000218 else {
Eric Christophere698f532012-12-20 21:58:36 +0000219 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000220 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000221 }
Eric Christopher05893f42013-12-30 18:32:31 +0000222 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
223 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000224}
225
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000226/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000227///
Eric Christophera5a79422013-12-09 23:32:48 +0000228void DwarfUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000229 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000230 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000231}
232
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000233/// addLabel - Add a Dwarf label attribute data and value.
234///
Eric Christophera5a79422013-12-09 23:32:48 +0000235void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
236 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000237 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
238 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000239}
240
Eric Christophera5a79422013-12-09 23:32:48 +0000241void DwarfUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
242 const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000243 addLabel(Die, (dwarf::Attribute)0, Form, Label);
244}
245
Eric Christopher33ff6972013-11-21 23:46:41 +0000246/// addSectionLabel - Add a Dwarf section label attribute data and value.
247///
Eric Christophera5a79422013-12-09 23:32:48 +0000248void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
249 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000250 if (DD->getDwarfVersion() >= 4)
251 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
252 else
253 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
254}
255
256/// addSectionOffset - Add an offset into a section attribute data and value.
257///
Eric Christophera5a79422013-12-09 23:32:48 +0000258void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
259 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000260 if (DD->getDwarfVersion() >= 4)
261 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
262 else
263 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
264}
265
Eric Christopher962c9082013-01-15 23:56:56 +0000266/// addLabelAddress - Add a dwarf label attribute data and value using
267/// DW_FORM_addr or DW_FORM_GNU_addr_index.
268///
Eric Christopher4287a492013-12-09 23:57:44 +0000269void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
270 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000271 if (Label)
272 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000273
Eric Christophercf48ade2014-01-24 11:52:53 +0000274 if (!DD->useSplitDwarf()) {
275 if (Label) {
276 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
277 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
278 } else {
279 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
280 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
281 }
Eric Christopher962c9082013-01-15 23:56:56 +0000282 } else {
Eric Christophercf48ade2014-01-24 11:52:53 +0000283 unsigned idx = DU->getAddrPoolIndex(Label);
284 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
285 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000286 }
287}
288
Eric Christophere9ec2452013-01-18 22:11:33 +0000289/// addOpAddress - Add a dwarf op address data and value using the
290/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
291///
Eric Christophera5a79422013-12-09 23:32:48 +0000292void DwarfUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000293 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000294 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
295 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000296 } else {
David Blaikief2443192013-10-21 17:28:37 +0000297 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
298 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000299 }
300}
301
Eric Christopher33ff6972013-11-21 23:46:41 +0000302/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000303///
Eric Christophera5a79422013-12-09 23:32:48 +0000304void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
305 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000306 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000307 if (DD->getDwarfVersion() >= 4)
308 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
309 else
310 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000311}
312
313/// addDIEEntry - Add a DIE attribute data and value.
314///
Eric Christophera5a79422013-12-09 23:32:48 +0000315void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000316 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
317}
318
David Blaikie47f615e2013-12-17 23:32:35 +0000319void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
320 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
321 new (DIEValueAllocator) DIETypeSignature(Type));
322}
323
Eric Christophera5a79422013-12-09 23:32:48 +0000324void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
325 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000326 const DIE *DieCU = Die->getUnitOrNull();
327 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000328 if (!DieCU)
329 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000330 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000331 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000332 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000333 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
334 : dwarf::DW_FORM_ref_addr,
335 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000336}
337
Manman Renb987e512013-10-29 00:53:03 +0000338/// Create a DIE with the given Tag, add the DIE to its parent, and
339/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000340DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000341 DIE *Die = new DIE(Tag);
342 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000343 if (N)
344 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000345 return Die;
346}
347
Devang Patel0e821f42011-04-12 23:21:44 +0000348/// addBlock - Add block data.
349///
Eric Christophera5a79422013-12-09 23:32:48 +0000350void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
351 DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000352 Block->ComputeSize(Asm);
353 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
354 Die->addValue(Attribute, Block->BestForm(), Block);
355}
356
357/// addSourceLine - Add location information to specified debug information
358/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000359void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
360 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000361 if (Line == 0)
362 return;
David Blaikie101613e2014-02-12 00:11:25 +0000363
David Blaikie5b858582014-02-12 00:40:47 +0000364 unsigned FileID =
365 DD->getOrCreateSourceID(File, Directory, getCU().getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000366 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000367 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
368 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000369}
370
371/// addSourceLine - Add location information to specified debug information
372/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000373void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
374 assert(V.isVariable());
375
Eric Christopher89a575c2014-02-12 22:38:04 +0000376 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
377 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000378}
379
380/// addSourceLine - Add location information to specified debug information
381/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000382void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000383 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000384
David Blaikie101613e2014-02-12 00:11:25 +0000385 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000386}
387
388/// addSourceLine - Add location information to specified debug information
389/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000390void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000391 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000392
David Blaikie101613e2014-02-12 00:11:25 +0000393 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000394}
395
396/// addSourceLine - Add location information to specified debug information
397/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000398void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000399 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000400
David Blaikie101613e2014-02-12 00:11:25 +0000401 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000402}
403
404/// addSourceLine - Add location information to specified debug information
405/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000406void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000407 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000408
Eric Christopher70e1bd82012-03-29 08:42:56 +0000409 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000410 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
411 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000412}
413
414/// addSourceLine - Add location information to specified debug information
415/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000416void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000417 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000418
David Blaikie101613e2014-02-12 00:11:25 +0000419 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000420}
421
Eric Christopher92331fd2012-11-21 00:34:38 +0000422/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000423/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000424void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
425 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000426 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000427 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000428 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000429 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
430 else
David Blaikieea2605d2013-06-20 00:25:24 +0000431 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000432 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000433}
434
Devang Patelba5fbf12011-04-26 19:06:18 +0000435/// addRegisterOp - Add register operand.
Eric Christophera5a79422013-12-09 23:32:48 +0000436void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000437 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000438 int DWReg = RI->getDwarfRegNum(Reg, false);
439 bool isSubRegister = DWReg < 0;
440
441 unsigned Idx = 0;
442
443 // Go up the super-register chain until we hit a valid dwarf register number.
444 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
445 DWReg = RI->getDwarfRegNum(*SR, false);
446 if (DWReg >= 0)
447 Idx = RI->getSubRegIndex(*SR, Reg);
448 }
449
450 if (DWReg < 0) {
451 DEBUG(llvm::dbgs() << "Invalid Dwarf register number.\n");
452 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
453 return;
454 }
455
456 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000457 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000458 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000459 else {
David Blaikief2443192013-10-21 17:28:37 +0000460 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
461 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000462 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000463
464 // Emit Mask
465 if (isSubRegister) {
466 unsigned Size = RI->getSubRegIdxSize(Idx);
467 unsigned Offset = RI->getSubRegIdxOffset(Idx);
468 if (Offset > 0) {
469 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
470 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
471 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
472 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000473 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000474 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000475 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000476 }
477 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000478}
479
480/// addRegisterOffset - Add register offset.
Eric Christophera5a79422013-12-09 23:32:48 +0000481void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
482 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000483 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
484 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
485 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
486 if (Reg == TRI->getFrameRegister(*Asm->MF))
487 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000488 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000489 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000490 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000491 else {
David Blaikief2443192013-10-21 17:28:37 +0000492 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
493 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000494 }
David Blaikief2443192013-10-21 17:28:37 +0000495 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000496}
497
498/// addAddress - Add an address attribute to a die based on the location
499/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000500void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
501 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000502 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
503
David Blaikieea2605d2013-06-20 00:25:24 +0000504 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000505 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000506 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000507 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000508 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000509 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000510 }
511 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000512
513 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000514 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000515}
516
Devang Patel0e821f42011-04-12 23:21:44 +0000517/// addComplexAddress - Start with the address based on the location provided,
518/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000519/// given the extra address information encoded in the DbgVariable, starting
520/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000521///
Eric Christophera5a79422013-12-09 23:32:48 +0000522void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
523 dwarf::Attribute Attribute,
524 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000525 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000526 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000527 unsigned i = 0;
528 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000529 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000530 // If first address element is OpPlus then emit
531 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000532 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000533 i = 2;
534 } else
535 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000536 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000537 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000538
Eric Christopherc2697f82013-10-19 01:04:47 +0000539 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000540 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000541 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000542 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
543 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000544 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000545 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000546 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000547 } else
548 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000549 }
550
551 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000552 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000553}
554
555/* Byref variables, in Blocks, are declared by the programmer as "SomeType
556 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
557 gives the variable VarName either the struct, or a pointer to the struct, as
558 its type. This is necessary for various behind-the-scenes things the
559 compiler needs to do with by-reference variables in Blocks.
560
561 However, as far as the original *programmer* is concerned, the variable
562 should still have type 'SomeType', as originally declared.
563
564 The function getBlockByrefType dives into the __Block_byref_x_VarName
565 struct to find the original type of the variable, which is then assigned to
566 the variable's Debug Information Entry as its real type. So far, so good.
567 However now the debugger will expect the variable VarName to have the type
568 SomeType. So we need the location attribute for the variable to be an
569 expression that explains to the debugger how to navigate through the
570 pointers and struct to find the actual variable of type SomeType.
571
572 The following function does just that. We start by getting
573 the "normal" location for the variable. This will be the location
574 of either the struct __Block_byref_x_VarName or the pointer to the
575 struct __Block_byref_x_VarName.
576
577 The struct will look something like:
578
579 struct __Block_byref_x_VarName {
580 ... <various fields>
581 struct __Block_byref_x_VarName *forwarding;
582 ... <various other fields>
583 SomeType VarName;
584 ... <maybe more fields>
585 };
586
587 If we are given the struct directly (as our starting point) we
588 need to tell the debugger to:
589
590 1). Add the offset of the forwarding field.
591
592 2). Follow that pointer to get the real __Block_byref_x_VarName
593 struct to use (the real one may have been copied onto the heap).
594
595 3). Add the offset for the field VarName, to find the actual variable.
596
597 If we started with a pointer to the struct, then we need to
598 dereference that pointer first, before the other steps.
599 Translating this into DWARF ops, we will need to append the following
600 to the current location description for the variable:
601
602 DW_OP_deref -- optional, if we start with a pointer
603 DW_OP_plus_uconst <forward_fld_offset>
604 DW_OP_deref
605 DW_OP_plus_uconst <varName_fld_offset>
606
607 That is what this function does. */
608
609/// addBlockByrefAddress - Start with the address based on the location
610/// provided, and generate the DWARF information necessary to find the
611/// actual Block variable (navigating the Block struct) based on the
612/// starting location. Add the DWARF information to the die. For
613/// more information, read large comment just above here.
614///
Eric Christophera5a79422013-12-09 23:32:48 +0000615void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
616 dwarf::Attribute Attribute,
617 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000618 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000619 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000620 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000621 bool isPointer = false;
622
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000623 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000624
625 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000626 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000627 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000628 isPointer = true;
629 }
630
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000631 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000632
633 // Find the __forwarding field and the variable field in the __Block_byref
634 // struct.
635 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000636 DIDerivedType varField;
637 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000638
639 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000640 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000641 StringRef fieldName = DT.getName();
642 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000643 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000644 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000645 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000646 }
647
648 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000649 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
650 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000651
652 // Decode the original location, and use that as the start of the byref
653 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000654 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
655
Eric Christopheref9d7102012-07-04 02:02:18 +0000656 if (Location.isReg())
657 addRegisterOp(Block, Location.getReg());
658 else
659 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000660
661 // If we started with a pointer to the __Block_byref... struct, then
662 // the first thing we need to do is dereference the pointer (DW_OP_deref).
663 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000664 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000665
666 // Next add the offset for the '__forwarding' field:
667 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
668 // adding the offset if it's 0.
669 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000670 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
671 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000672 }
673
674 // Now dereference the __forwarding field to get to the real __Block_byref
675 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000676 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000677
678 // Now that we've got the real __Block_byref... struct, add the offset
679 // for the variable's field to get to the location of the actual variable:
680 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
681 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000682 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
683 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000684 }
685
686 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000687 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000688}
689
Devang Patelbcd50a12011-07-20 21:57:04 +0000690/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000691static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000692 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000693 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
694 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000695 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000696 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
697 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000698 *SizeInBits = Ty.getSizeInBits();
699 return true;
700 }
701 return false;
702}
703
Manman Renb3388602013-10-05 01:43:03 +0000704/// Return true if type encoding is unsigned.
705static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
706 DIDerivedType DTy(Ty);
707 if (DTy.isDerivedType())
708 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
709
710 DIBasicType BTy(Ty);
711 if (BTy.isBasicType()) {
712 unsigned Encoding = BTy.getEncoding();
713 if (Encoding == dwarf::DW_ATE_unsigned ||
714 Encoding == dwarf::DW_ATE_unsigned_char ||
715 Encoding == dwarf::DW_ATE_boolean)
716 return true;
717 }
718 return false;
719}
720
721/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000722static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000723 unsigned Tag = Ty.getTag();
724
725 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
726 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
727 Tag != dwarf::DW_TAG_restrict_type)
728 return Ty.getSizeInBits();
729
730 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
731
732 // If this type is not derived from any type then take conservative approach.
733 if (!BaseType.isValid())
734 return Ty.getSizeInBits();
735
736 // If this is a derived type, go ahead and get the base type, unless it's a
737 // reference then it's just the size of the field. Pointer types have no need
738 // of this since they're a different type of qualification on the type.
739 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
740 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
741 return Ty.getSizeInBits();
742
743 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000744 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000745
746 return BaseType.getSizeInBits();
747}
748
Devang Patel0e821f42011-04-12 23:21:44 +0000749/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000750void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
751 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000752 // FIXME: This is a bit conservative/simple - it emits negative values at
753 // their maximum bit width which is a bit unfortunate (& doesn't prefer
754 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000755 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000756 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000757 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000758 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000759
Eric Christopher9d1daa82013-08-27 23:49:04 +0000760 // If we're a signed constant definitely use sdata.
761 if (SignedConstant) {
762 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
763 return;
764 }
765
766 // Else use data for now unless it's larger than we can deal with.
767 switch (SizeInBits) {
768 case 8:
769 Form = dwarf::DW_FORM_data1;
770 break;
771 case 16:
772 Form = dwarf::DW_FORM_data2;
773 break;
774 case 32:
775 Form = dwarf::DW_FORM_data4;
776 break;
777 case 64:
778 Form = dwarf::DW_FORM_data8;
779 break;
780 default:
781 Form = dwarf::DW_FORM_udata;
782 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
783 return;
784 }
785 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000786}
787
788/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000789void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000790 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000791 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
792 APFloat FPImm = MO.getFPImm()->getValueAPF();
793
794 // Get the raw data form of the floating point.
795 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000796 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000797
798 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000799 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000800 int Incr = (LittleEndian ? 1 : -1);
801 int Start = (LittleEndian ? 0 : NumBytes - 1);
802 int Stop = (LittleEndian ? NumBytes : -1);
803
804 // Output the constant to DWARF one byte at a time.
805 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000806 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000807
David Blaikief2443192013-10-21 17:28:37 +0000808 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000809}
810
David Blaikiea39a76e2013-01-20 01:18:01 +0000811/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000812void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000813 // Pass this down to addConstantValue as an unsigned bag of bits.
814 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000815}
816
Devang Patel0e821f42011-04-12 23:21:44 +0000817/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000818void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
819 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000820 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000821}
822
823// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000824void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000825 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000826 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000827 // If we're a signed constant definitely use sdata.
828 if (!Unsigned) {
829 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
830 Val.getSExtValue());
831 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000832 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000833
834 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000835 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000836 switch (CIBitWidth) {
837 case 8:
838 Form = dwarf::DW_FORM_data1;
839 break;
840 case 16:
841 Form = dwarf::DW_FORM_data2;
842 break;
843 case 32:
844 Form = dwarf::DW_FORM_data4;
845 break;
846 case 64:
847 Form = dwarf::DW_FORM_data8;
848 break;
849 default:
850 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
851 Val.getZExtValue());
852 return;
853 }
854 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000855 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000856 }
857
858 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
859
860 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000861 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000862
863 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000864 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000865
866 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000867 for (int i = 0; i < NumBytes; i++) {
868 uint8_t c;
869 if (LittleEndian)
870 c = Ptr64[i / 8] >> (8 * (i & 7));
871 else
872 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000873 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000874 }
Devang Patel0e821f42011-04-12 23:21:44 +0000875
David Blaikief2443192013-10-21 17:28:37 +0000876 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000877}
878
Eric Christopher25e35092013-04-22 07:47:40 +0000879/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000880void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000881 // Add template parameters.
882 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
883 DIDescriptor Element = TParams.getElement(i);
884 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000885 constructTemplateTypeParameterDIE(Buffer,
886 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000887 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000888 constructTemplateValueParameterDIE(Buffer,
889 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000890 }
Devang Patel0e821f42011-04-12 23:21:44 +0000891}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000892
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000893/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000894DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000895 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000896 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000897 if (Context.isType())
898 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000899 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000900 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000901 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000902 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000903 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000904}
905
Eric Christophera5a79422013-12-09 23:32:48 +0000906DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000907 DIScope Context = resolve(Ty.getContext());
908 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000909
910 DIE *TyDIE = getDIE(Ty);
911 if (TyDIE)
912 return TyDIE;
913
914 // Create new type.
915 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
916
David Blaikiefbd29eb2013-11-26 00:35:04 +0000917 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000918
David Blaikie9d861be2013-11-26 00:15:27 +0000919 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000920 return TyDIE;
921}
922
Devang Patel0e821f42011-04-12 23:21:44 +0000923/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
924/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000925DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000926 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000927 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000928
David Blaikie32887552013-11-14 22:25:02 +0000929 DIType Ty(TyNode);
930 assert(Ty.isType());
931
Manman Renf4c339e2013-10-29 22:49:29 +0000932 // Construct the context before querying for the existence of the DIE in case
933 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000934 DIScope Context = resolve(Ty.getContext());
935 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000936 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000937
Eric Christophere595bae2013-10-04 17:08:38 +0000938 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000939 if (TyDIE)
940 return TyDIE;
941
942 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000943 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
944
Devang Patel0e821f42011-04-12 23:21:44 +0000945 if (Ty.isBasicType())
946 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000947 else if (Ty.isCompositeType()) {
948 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000949 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
950 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +0000951 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
David Blaikiecfb21152014-01-03 18:59:42 +0000952 // Skip updating the accellerator tables since this is not the full type
953 return TyDIE;
954 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000955 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000956 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000957 assert(Ty.isDerivedType() && "Unknown kind of DIType");
958 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
959 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000960
David Blaikie9d861be2013-11-26 00:15:27 +0000961 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +0000962
963 return TyDIE;
964}
965
Eric Christophera5a79422013-12-09 23:32:48 +0000966void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
967 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000968 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
969 bool IsImplementation = 0;
970 if (Ty.isCompositeType()) {
971 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000972 // A runtime language of 0 actually means C/C++ and that any
973 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +0000974 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000975 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000976 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000977 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +0000978
979 if (!Context || Context.isCompileUnit() || Context.isFile() ||
980 Context.isNameSpace())
981 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +0000982 }
Devang Patel0e821f42011-04-12 23:21:44 +0000983}
984
985/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +0000986void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000987 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000988
989 // Check for pre-existence.
990 DIEEntry *Entry = getDIEEntry(Ty);
991 // If it exists then use the existing value.
992 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000993 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000994 return;
995 }
996
997 // Construct type.
998 DIE *Buffer = getOrCreateTypeDIE(Ty);
999
1000 // Set up proxy.
1001 Entry = createDIEEntry(Buffer);
1002 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001003 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001004}
1005
Eric Christopher9cd26af2013-09-20 23:22:52 +00001006// Accelerator table mutators - add each name along with its companion
1007// DIE to the proper table while ensuring that the name that we're going
1008// to reference is in the string table. We do this since the names we
1009// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001010void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001011 if (!DD->useDwarfAccelTables())
1012 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001013 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001014 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001015 DIEs.push_back(Die);
1016}
1017
Eric Christophera5a79422013-12-09 23:32:48 +00001018void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001019 if (!DD->useDwarfAccelTables())
1020 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001021 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001022 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001023 DIEs.push_back(Die);
1024}
1025
Eric Christophera5a79422013-12-09 23:32:48 +00001026void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001027 if (!DD->useDwarfAccelTables())
1028 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001029 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001030 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001031 DIEs.push_back(Die);
1032}
1033
Eric Christophera5a79422013-12-09 23:32:48 +00001034void DwarfUnit::addAccelType(StringRef Name,
1035 std::pair<const DIE *, unsigned> Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001036 if (!DD->useDwarfAccelTables())
1037 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001038 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001039 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001040 DIEs.push_back(Die);
1041}
1042
Eric Christopher9c58f312013-09-20 22:20:55 +00001043/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001044void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001045 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001046 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001047}
1048
Eric Christopher2c8b7902013-10-17 02:06:06 +00001049/// getParentContextString - Walks the metadata parent chain in a language
1050/// specific manner (using the compile unit language) and returns
1051/// it as a string. This is done at the metadata level because DIEs may
1052/// not currently have been added to the parent context and walking the
1053/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001054std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001055 if (!Context)
1056 return "";
1057
1058 // FIXME: Decide whether to implement this for non-C++ languages.
1059 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1060 return "";
1061
Eric Christopher8dba0d52013-10-19 01:04:42 +00001062 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001063 SmallVector<DIScope, 1> Parents;
1064 while (!Context.isCompileUnit()) {
1065 Parents.push_back(Context);
1066 if (Context.getContext())
1067 Context = resolve(Context.getContext());
1068 else
1069 // Structure, etc types will have a NULL context if they're at the top
1070 // level.
1071 break;
1072 }
1073
1074 // Reverse iterate over our list to go from the outermost construct to the
1075 // innermost.
1076 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1077 E = Parents.rend();
1078 I != E; ++I) {
1079 DIScope Ctx = *I;
1080 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001081 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001082 CS += Name;
1083 CS += "::";
1084 }
1085 }
1086 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001087}
1088
1089/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001090void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001091 // Get core information.
1092 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001093 // Add name if not anonymous or intermediate type.
1094 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001095 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001096
David Blaikiefac56122013-10-04 23:21:16 +00001097 // An unspecified type only has a name attribute.
1098 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001099 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001100
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001101 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001102 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001103
Devang Patel0e821f42011-04-12 23:21:44 +00001104 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001105 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001106}
1107
1108/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001109void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001110 // Get core information.
1111 StringRef Name = DTy.getName();
1112 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001113 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001114
1115 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001116 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001117 if (FromTy)
1118 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001119
1120 // Add name if not anonymous or intermediate type.
1121 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001122 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001123
1124 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001125 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001126 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001127
David Blaikie5d3249b2013-01-07 05:51:15 +00001128 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001129 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1130 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001131 // Add source line info if available and TyDesc is not a forward declaration.
1132 if (!DTy.isForwardDecl())
1133 addSourceLine(&Buffer, DTy);
1134}
1135
1136/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001137void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001138 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001139 StringRef Name = CTy.getName();
1140
1141 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001142 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001143
1144 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001145 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001146 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001147 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001148 case dwarf::DW_TAG_enumeration_type:
1149 constructEnumTypeDIE(Buffer, CTy);
1150 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001151 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001152 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001153 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001154 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001155 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001156 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001157
1158 bool isPrototyped = true;
1159 // Add arguments.
1160 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1161 DIDescriptor Ty = Elements.getElement(i);
1162 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001163 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001164 isPrototyped = false;
1165 } else {
Manman Renb987e512013-10-29 00:53:03 +00001166 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001167 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001168 if (DIType(Ty).isArtificial())
1169 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001170 }
1171 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001172 // Add prototype flag if we're dealing with a C language and the
1173 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001174 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001175 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001176 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001177 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001178 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001179
1180 if (CTy.isLValueReference())
1181 addFlag(&Buffer, dwarf::DW_AT_reference);
1182
1183 if (CTy.isRValueReference())
1184 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001185 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001186 case dwarf::DW_TAG_structure_type:
1187 case dwarf::DW_TAG_union_type:
1188 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001189 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001190 DIArray Elements = CTy.getTypeArray();
1191 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001192 DIDescriptor Element = Elements.getElement(i);
1193 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001194 if (Element.isSubprogram())
1195 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1196 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001197 DIDerivedType DDTy(Element);
1198 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001199 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001200 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001201 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001202 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001203 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001204 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001205 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001206 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001207 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001208 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001209 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001210 StringRef PropertyName = Property.getObjCPropertyName();
1211 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001212 if (Property.getType())
1213 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001214 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001215 StringRef GetterName = Property.getObjCPropertyGetterName();
1216 if (!GetterName.empty())
1217 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1218 StringRef SetterName = Property.getObjCPropertySetterName();
1219 if (!SetterName.empty())
1220 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1221 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001222 if (Property.isReadOnlyObjCProperty())
1223 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1224 if (Property.isReadWriteObjCProperty())
1225 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1226 if (Property.isAssignObjCProperty())
1227 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1228 if (Property.isRetainObjCProperty())
1229 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1230 if (Property.isCopyObjCProperty())
1231 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1232 if (Property.isNonAtomicObjCProperty())
1233 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1234 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001235 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001236 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001237
Devang Pateld925d1a2012-02-07 23:33:58 +00001238 DIEEntry *Entry = getDIEEntry(Element);
1239 if (!Entry) {
1240 Entry = createDIEEntry(ElemDie);
1241 insertDIEEntry(Element, Entry);
1242 }
Devang Patel403e8192012-02-04 01:30:32 +00001243 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001244 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001245 }
1246
1247 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001248 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001249
Eric Christopher9e429ae2013-10-05 00:27:02 +00001250 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001251 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001252 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001253 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001254
Devang Patel12419ae2011-05-12 21:29:42 +00001255 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001256 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001257
Eric Christopherda011dd2011-12-16 23:42:42 +00001258 // Add template parameters to a class, structure or union types.
1259 // FIXME: The support isn't in the metadata for this yet.
1260 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001261 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001262 addTemplateParams(Buffer, CTy.getTemplateParams());
1263
1264 break;
1265 }
1266 default:
1267 break;
1268 }
1269
1270 // Add name if not anonymous or intermediate type.
1271 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001272 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001273
Eric Christopher775cbd22012-05-22 18:45:18 +00001274 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001275 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001276 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001277 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001278 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001279 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001280 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001281 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001282 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001283 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001284
1285 // If we're a forward decl, say so.
1286 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001287 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001288
1289 // Add source line info if available.
1290 if (!CTy.isForwardDecl())
1291 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001292
1293 // No harm in adding the runtime language to the declaration.
1294 unsigned RLang = CTy.getRunTimeLang();
1295 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001296 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1297 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001298 }
1299}
1300
Manman Renffc9a712013-10-23 23:05:28 +00001301/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1302/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001303void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1304 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001305 DIE *ParamDIE =
1306 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001307 // Add the type if it exists, it could be void and therefore no type.
1308 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001309 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001310 if (!TP.getName().empty())
1311 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001312}
1313
Manman Renffc9a712013-10-23 23:05:28 +00001314/// constructTemplateValueParameterDIE - Construct new DIE for the given
1315/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001316void
1317DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001318 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001319 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001320
1321 // Add the type if there is one, template template and template parameter
1322 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001323 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001324 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001325 if (!VP.getName().empty())
1326 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1327 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001328 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001329 addConstantValue(ParamDIE, CI,
1330 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001331 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1332 // For declaration non-type template parameters (such as global values and
1333 // functions)
1334 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001335 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001336 // Emit DW_OP_stack_value to use the address as the immediate value of the
1337 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001338 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1339 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001340 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001341 assert(isa<MDString>(Val));
1342 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1343 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001344 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001345 assert(isa<MDNode>(Val));
1346 DIArray A(cast<MDNode>(Val));
1347 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001348 }
1349 }
Devang Patel0e821f42011-04-12 23:21:44 +00001350}
1351
Devang Patel17b53272011-05-06 16:57:54 +00001352/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001353DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001354 // Construct the context before querying for the existence of the DIE in case
1355 // such construction creates the DIE.
1356 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001357
Devang Patel17b53272011-05-06 16:57:54 +00001358 DIE *NDie = getDIE(NS);
1359 if (NDie)
1360 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001361 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1362
Eric Christopher4996c702011-11-07 09:24:32 +00001363 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001364 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001365 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001366 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001367 } else
1368 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001369 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001370 return NDie;
1371}
1372
Devang Patel89543712011-08-15 17:24:54 +00001373/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001374DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001375 // Construct the context before querying for the existence of the DIE in case
1376 // such construction creates the DIE (as is the case for member function
1377 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001378 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001379
Eric Christophere595bae2013-10-04 17:08:38 +00001380 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001381 if (SPDie)
1382 return SPDie;
1383
Manman Ren73d697c2013-10-29 00:58:04 +00001384 DISubprogram SPDecl = SP.getFunctionDeclaration();
1385 if (SPDecl.isSubprogram())
1386 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001387 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001388
1389 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001390 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001391
Rafael Espindola79278362011-11-10 22:34:29 +00001392 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001393 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001394 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001395
Devang Patel89543712011-08-15 17:24:54 +00001396 // Add function template parameters.
1397 addTemplateParams(*SPDie, SP.getTemplateParams());
1398
Devang Patel89543712011-08-15 17:24:54 +00001399 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001400 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001401 if (DeclDie) {
1402 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001403 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001404
Devang Patel89543712011-08-15 17:24:54 +00001405 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001406 }
Devang Patel89543712011-08-15 17:24:54 +00001407
Eric Christopheracb71152012-08-23 22:52:55 +00001408 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001409 StringRef LinkageName = SP.getLinkageName();
1410 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001411 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001412 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001413
Devang Patel89543712011-08-15 17:24:54 +00001414 // Constructors and operators for anonymous aggregates do not have names.
1415 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001416 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001417
1418 addSourceLine(SPDie, SP);
1419
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001420 // Add the prototype if we have a prototype and we have a C like
1421 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001422 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001423 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001424 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001425 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001426 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001427
Devang Patel89543712011-08-15 17:24:54 +00001428 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001429 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1430 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001431
David Blaikie5174c842013-05-22 23:22:18 +00001432 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001433 // Add a return type. If this is a type like a C/C++ void type we don't add a
1434 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001435 if (Args.getElement(0))
1436 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001437
1438 unsigned VK = SP.getVirtuality();
1439 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001440 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001441 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001442 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1443 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1444 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001445 ContainingTypeMap.insert(
1446 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001447 }
1448
1449 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001450 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001451
Devang Patel89543712011-08-15 17:24:54 +00001452 // Add arguments. Do not add arguments for subprogram definition. They will
1453 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001454 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001455 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001456 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001457 addType(Arg, ATy);
1458 if (ATy.isArtificial())
1459 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001460 }
Devang Patel89543712011-08-15 17:24:54 +00001461 }
1462
1463 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001464 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001465
1466 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001467 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001468
1469 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001470 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001471
1472 if (unsigned isa = Asm->getISAEncoding()) {
1473 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1474 }
1475
Adrian Prantl99c7af22013-12-18 21:48:19 +00001476 if (SP.isLValueReference())
1477 addFlag(SPDie, dwarf::DW_AT_reference);
1478
1479 if (SP.isRValueReference())
1480 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1481
Adrian Prantlef129fb2014-01-18 02:12:00 +00001482 if (SP.isProtected())
1483 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1484 dwarf::DW_ACCESS_protected);
1485 else if (SP.isPrivate())
1486 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1487 dwarf::DW_ACCESS_private);
1488 else
1489 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1490 dwarf::DW_ACCESS_public);
1491
1492 if (SP.isExplicit())
1493 addFlag(SPDie, dwarf::DW_AT_explicit);
1494
Devang Patel89543712011-08-15 17:24:54 +00001495 return SPDie;
1496}
1497
Devang Pateldfd6ec32011-08-15 17:57:41 +00001498// Return const expression if value is a GEP to access merged global
1499// constant. e.g.
1500// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1501static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1502 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1503 if (!CE || CE->getNumOperands() != 3 ||
1504 CE->getOpcode() != Instruction::GetElementPtr)
1505 return NULL;
1506
1507 // First operand points to a global struct.
1508 Value *Ptr = CE->getOperand(0);
1509 if (!isa<GlobalValue>(Ptr) ||
1510 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1511 return NULL;
1512
1513 // Second operand is zero.
1514 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1515 if (!CI || !CI->isZero())
1516 return NULL;
1517
1518 // Third operand is offset.
1519 if (!isa<ConstantInt>(CE->getOperand(2)))
1520 return NULL;
1521
1522 return CE;
1523}
1524
1525/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001526void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001527 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001528 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001529 return;
1530
David Blaikie5e390e42014-02-04 01:23:52 +00001531 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001532
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001533 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001534 DIType GTy = GV.getType();
1535
1536 // If this is a static data member definition, some attributes belong
1537 // to the declaration DIE.
1538 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001539 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001540 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1541 if (SDMDecl.Verify()) {
1542 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1543 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001544 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001545 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001546 }
1547
1548 // If this is not a static data member definition, create the variable
1549 // DIE and add the initial set of attributes to it.
1550 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001551 // Construct the context before querying for the existence of the DIE in
1552 // case such construction creates the DIE.
1553 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001554
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001555 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001556 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001557
1558 // Add name and type.
1559 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1560 addType(VariableDIE, GTy);
1561
1562 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001563 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001564 addFlag(VariableDIE, dwarf::DW_AT_external);
1565
1566 // Add line number info.
1567 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001568 }
1569
Devang Pateldfd6ec32011-08-15 17:57:41 +00001570 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001571 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001572 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001573 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001574 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001575 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001576 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001577 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001578 if (GV.getGlobal()->isThreadLocal()) {
1579 // FIXME: Make this work with -gsplit-dwarf.
1580 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1581 assert((PointerSize == 4 || PointerSize == 8) &&
1582 "Add support for other sizes if necessary");
1583 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001584 if (!DD->useSplitDwarf()) {
1585 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001586 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001587 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001588 // 2) containing the (relocated) offset of the TLS variable
1589 // within the module's TLS block.
David Blaikief1a6dea2014-02-15 19:34:03 +00001590 addExpr(Block, dwarf::DW_FORM_udata,
1591 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001592 } else {
David Blaikief2443192013-10-21 17:28:37 +00001593 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
David Blaikief1a6dea2014-02-15 19:34:03 +00001594 addUInt(Block, dwarf::DW_FORM_udata,
1595 DU->getAddrPoolIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001596 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001597 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001598 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001599 } else {
1600 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001601 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001602 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001603 // Do not create specification DIE if context is either compile unit
1604 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001605 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001606 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001607 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001608 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001609 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001610 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001611 // A static member's declaration is already flagged as such.
1612 if (!SDMDecl.Verify())
1613 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001614 } else {
David Blaikief2443192013-10-21 17:28:37 +00001615 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001616 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001617 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001618 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001619 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001620 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1621 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1622 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001623 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1624 : VariableDIE,
1625 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001626 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001627 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001628 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001629 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001630 // emitting AT_const_value multiple times, we only add AT_const_value when
1631 // it is not a static member.
1632 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001633 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001634 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001635 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001636 // GV is a merged global.
1637 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1638 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001639 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1640 DD->addArangeLabel(SymbolCU(this, Sym));
1641 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001642 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001643 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001644 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001645 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001646 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1647 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001648 }
1649
Eric Christopherc12c2112011-11-11 01:55:22 +00001650 if (addToAccelTable) {
1651 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1652 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001653
Eric Christopherc12c2112011-11-11 01:55:22 +00001654 // If the linkage name is different than the name, go ahead and output
1655 // that as well into the name table.
1656 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1657 addAccelName(GV.getLinkageName(), AddrDIE);
1658 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001659
1660 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001661 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1662 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001663}
1664
Devang Patel0e821f42011-04-12 23:21:44 +00001665/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001666void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001667 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001668 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001669
Bill Wendling28fe9e72012-12-06 07:38:10 +00001670 // The LowerBound value defines the lower bounds which is typically zero for
1671 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1672 // Count == -1 then the array is unbounded and we do not emit
1673 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1674 // Count == 0, then the array has zero elements in which case we do not emit
1675 // an upper bound.
1676 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001677 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001678 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001679
Bill Wendling3495f9b2012-12-06 07:55:19 +00001680 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001681 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001682
1683 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001684 // FIXME: An unbounded array should reference the expression that defines
1685 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001686 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1687 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001688}
1689
1690/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001691void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001692 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001693 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001694
Eric Christopher0df08e22013-08-08 07:40:37 +00001695 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001696 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001697
1698 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001699 // FIXME: This type should be passed down from the front end
1700 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001701 DIE *IdxTy = getIndexTyDie();
1702 if (!IdxTy) {
1703 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001704 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001705 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001706 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001707 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1708 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001709 setIndexTyDie(IdxTy);
1710 }
1711
1712 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001713 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001714 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1715 DIDescriptor Element = Elements.getElement(i);
1716 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1717 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1718 }
1719}
1720
Eric Christopheraeb105f2013-11-11 18:52:39 +00001721/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001722void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001723 DIArray Elements = CTy.getTypeArray();
1724
1725 // Add enumerators to enumeration type.
1726 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001727 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001728 if (Enum.isEnumerator()) {
1729 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001730 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001731 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001732 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001733 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1734 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001735 }
1736 }
1737 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1738 if (DTy) {
1739 addType(&Buffer, DTy);
1740 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1741 }
Devang Patel0e821f42011-04-12 23:21:44 +00001742}
1743
Devang Patel89543712011-08-15 17:24:54 +00001744/// constructContainingTypeDIEs - Construct DIEs for types that contain
1745/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001746void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001747 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001748 CE = ContainingTypeMap.end();
1749 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001750 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001751 DIDescriptor D(CI->second);
1752 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001753 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001754 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001755 if (!NDie)
1756 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001757 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001758 }
1759}
1760
Devang Patel3acc70e2011-08-15 22:04:40 +00001761/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001762DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001763 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001764
Devang Patel3acc70e2011-08-15 22:04:40 +00001765 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001766 DIE *VariableDie = new DIE(DV.getTag());
1767 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001768 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001769 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001770 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001771 else {
David Blaikie715528b2013-08-19 03:34:03 +00001772 if (!Name.empty())
1773 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001774 addSourceLine(VariableDie, DV.getVariable());
1775 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001776 }
1777
Eric Christopher34a2c872013-11-15 01:43:19 +00001778 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001779 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001780
1781 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001782 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001783 return VariableDie;
1784 }
1785
1786 // Add variable address.
1787
Eric Christopher34a2c872013-11-15 01:43:19 +00001788 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001789 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001790 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1791 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001792 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001793 return VariableDie;
1794 }
1795
Eric Christophercead0332011-10-03 15:49:20 +00001796 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001797 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001798 assert(DVInsn->getNumOperands() == 3);
1799 if (DVInsn->getOperand(0).isReg()) {
1800 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001801 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001802 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001803 MachineLocation Location(RegOp.getReg(),
1804 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001805 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001806 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001807 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001808 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001809 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001810 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001811 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001812 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001813 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001814 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001815
Eric Christopher34a2c872013-11-15 01:43:19 +00001816 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001817 return VariableDie;
1818 } else {
1819 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001820 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001821 if (FI != ~0) {
1822 unsigned FrameReg = 0;
1823 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001824 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001825 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001826 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001827 }
1828 }
1829
Eric Christopher34a2c872013-11-15 01:43:19 +00001830 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001831 return VariableDie;
1832}
1833
Manman Ren230ec862013-10-23 23:00:44 +00001834/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001835void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001836 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001837 StringRef Name = DT.getName();
1838 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001839 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001840
Manman Ren93b30902013-10-08 18:42:58 +00001841 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001842
1843 addSourceLine(MemberDie, DT);
1844
Eric Christopherc2697f82013-10-19 01:04:47 +00001845 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001846
1847 // For C++, virtual base classes are not at fixed offset. Use following
1848 // expression to extract appropriate offset from vtable.
1849 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1850
1851 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001852 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1853 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1854 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1855 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1856 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1857 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1858 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001859
David Blaikief2443192013-10-21 17:28:37 +00001860 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001861 } else {
1862 uint64_t Size = DT.getSizeInBits();
1863 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1864 uint64_t OffsetInBytes;
1865
1866 if (Size != FieldSize) {
1867 // Handle bitfield.
1868 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1869 getBaseTypeSize(DD, DT) >> 3);
1870 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1871
1872 uint64_t Offset = DT.getOffsetInBits();
1873 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1874 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1875 uint64_t FieldOffset = (HiMark - FieldSize);
1876 Offset -= FieldOffset;
1877
1878 // Maybe we need to work from the other end.
1879 if (Asm->getDataLayout().isLittleEndian())
1880 Offset = FieldSize - (Offset + Size);
1881 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1882
Adrian Prantlc67655a2014-01-28 18:13:47 +00001883 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001884 // field that includes this bit field.
1885 OffsetInBytes = FieldOffset >> 3;
1886 } else
1887 // This is not a bitfield.
1888 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001889
David Blaikie22b29a52014-01-03 01:30:05 +00001890 if (DD->getDwarfVersion() <= 2) {
1891 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1892 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1893 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1894 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1895 } else
1896 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1897 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001898 }
Devang Patel0e821f42011-04-12 23:21:44 +00001899
1900 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001901 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001902 dwarf::DW_ACCESS_protected);
1903 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001904 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001905 dwarf::DW_ACCESS_private);
1906 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001907 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001908 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001909 dwarf::DW_ACCESS_public);
1910 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001911 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001912 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001913
1914 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001915 if (MDNode *PNode = DT.getObjCProperty())
1916 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001917 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001918 PropertyDie);
1919
David Blaikie37fefc32012-12-13 22:43:07 +00001920 if (DT.isArtificial())
1921 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001922}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001923
Manman Renc6b63922013-10-14 20:33:57 +00001924/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001925DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001926 if (!DT.Verify())
1927 return NULL;
1928
Manman Renc6b63922013-10-14 20:33:57 +00001929 // Construct the context before querying for the existence of the DIE in case
1930 // such construction creates the DIE.
1931 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001932 assert(dwarf::isType(ContextDIE->getTag()) &&
1933 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001934
1935 DIE *StaticMemberDIE = getDIE(DT);
1936 if (StaticMemberDIE)
1937 return StaticMemberDIE;
1938
Manman Renb987e512013-10-29 00:53:03 +00001939 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001940
Manman Ren93b30902013-10-08 18:42:58 +00001941 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001942
1943 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1944 addType(StaticMemberDIE, Ty);
1945 addSourceLine(StaticMemberDIE, DT);
1946 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1947 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1948
1949 // FIXME: We could omit private if the parent is a class_type, and
1950 // public if the parent is something else.
1951 if (DT.isProtected())
1952 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1953 dwarf::DW_ACCESS_protected);
1954 else if (DT.isPrivate())
1955 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1956 dwarf::DW_ACCESS_private);
1957 else
1958 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1959 dwarf::DW_ACCESS_public);
1960
1961 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001962 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001963 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1964 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001965
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001966 return StaticMemberDIE;
1967}
David Blaikie6b288cf2013-10-30 20:42:41 +00001968
Eric Christophera5a79422013-12-09 23:32:48 +00001969void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00001970 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001971 Asm->OutStreamer.AddComment("DWARF version number");
1972 Asm->EmitInt16(DD->getDwarfVersion());
1973 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001974 // We share one abbreviations table across all units so it's always at the
1975 // start of the section. Use a relocatable offset where needed to ensure
1976 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00001977 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001978 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1979 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1980}
David Blaikiebc563272013-12-13 21:33:40 +00001981
David Blaikie2494fdb2014-02-14 22:41:51 +00001982void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
1983 // Define start line table label for each Compile Unit.
1984 MCSymbol *LineTableStartSym =
1985 Asm->GetTempSymbol("line_table_start", getUniqueID());
1986 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
1987 getUniqueID());
1988
1989 // Use a single line table if we are generating assembly.
1990 bool UseTheFirstCU =
1991 Asm->OutStreamer.hasRawTextSupport() || (getUniqueID() == 0);
1992
David Blaikie60e63862014-02-14 23:58:13 +00001993 stmtListIndex = UnitDie->getValues().size();
1994
David Blaikie2494fdb2014-02-14 22:41:51 +00001995 // DW_AT_stmt_list is a offset of line number information for this
1996 // compile unit in debug_line section. For split dwarf this is
1997 // left in the skeleton CU and so not included.
1998 // The line table entries are not always emitted in assembly, so it
1999 // is not okay to use line_table_start here.
2000 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2001 addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list,
2002 UseTheFirstCU ? DwarfLineSectionSym : LineTableStartSym);
2003 else if (UseTheFirstCU)
2004 addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
2005 else
2006 addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2007 DwarfLineSectionSym);
2008}
2009
David Blaikie60e63862014-02-14 23:58:13 +00002010void DwarfCompileUnit::applyStmtList(DIE &D) {
2011 D.addValue(dwarf::DW_AT_stmt_list,
2012 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2013 UnitDie->getValues()[stmtListIndex]);
2014}
2015
David Blaikiebc563272013-12-13 21:33:40 +00002016void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2017 const MCSymbol *ASectionSym) const {
2018 DwarfUnit::emitHeader(ASection, ASectionSym);
2019 Asm->OutStreamer.AddComment("Type Signature");
2020 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2021 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002022 // In a skeleton type unit there is no type DIE so emit a zero offset.
2023 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2024 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002025}
2026
2027void DwarfTypeUnit::initSection(const MCSection *Section) {
2028 assert(!this->Section);
2029 this->Section = Section;
2030 // Since each type unit is contained in its own COMDAT section, the begin
2031 // label and the section label are the same. Using the begin label emission in
2032 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2033 // the only other alternative of lazily constructing start-of-section labels
2034 // and storing a mapping in DwarfDebug (or AsmPrinter).
2035 this->SectionSym = this->LabelBegin =
2036 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2037 this->LabelEnd =
2038 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2039 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2040}