blob: b636a5dab64878b8d723ddce72bd411bb66b7d69 [file] [log] [blame]
Devang Patel0e821f42011-04-12 23:21:44 +00001//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
2//
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
16#include "DwarfCompileUnit.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"
David Blaikie6b288cf2013-10-30 20:42:41 +000025#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCStreamer.h"
Devang Pateldfd6ec32011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel0e821f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
David Blaikief2694972013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetRegisterInfo.h"
David Blaikie409dd9c2013-11-19 23:08:21 +000032#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000033
34using namespace llvm;
35
David Blaikie409dd9c2013-11-19 23:08:21 +000036static cl::opt<bool> GenerateTypeUnits("generate-type-units", cl::Hidden,
37 cl::desc("Generate DWARF4 type units."),
38 cl::init(false));
39
Devang Patel0e821f42011-04-12 23:21:44 +000040/// CompileUnit - Compile unit constructor.
David Blaikie5a152402013-11-15 23:52:02 +000041CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
42 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
Eric Christopherbca5c632013-11-21 01:14:00 +000043 : UniqueID(UID), Node(Node), Language(Node.getLanguage()), CUDie(D), Asm(A),
44 DD(DW), DU(DWU), IndexTyDie(0), DebugInfoOffset(0) {
Devang Patel0e821f42011-04-12 23:21:44 +000045 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
David Blaikie5a152402013-11-15 23:52:02 +000046 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000047}
48
David Blaikie409dd9c2013-11-19 23:08:21 +000049CompileUnit::CompileUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
50 DwarfDebug *DD, DwarfUnits *DU)
Eric Christopherbca5c632013-11-21 01:14:00 +000051 : UniqueID(UID), Node(NULL), Language(Language), CUDie(D), Asm(A), DD(DD),
52 DU(DU), IndexTyDie(0), DebugInfoOffset(0) {
David Blaikie409dd9c2013-11-19 23:08:21 +000053 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
54}
55
Devang Patel0e821f42011-04-12 23:21:44 +000056/// ~CompileUnit - Destructor for compile unit.
57CompileUnit::~CompileUnit() {
58 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
59 DIEBlocks[j]->~DIEBlock();
60}
61
62/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
63/// information entry.
64DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
65 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
66 return Value;
67}
68
Bill Wendling3495f9b2012-12-06 07:55:19 +000069/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000070/// DWARF version doesn't handle the language, return -1.
Bill Wendling3495f9b2012-12-06 07:55:19 +000071int64_t CompileUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000072 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000073 default:
74 break;
75
76 case dwarf::DW_LANG_C89:
77 case dwarf::DW_LANG_C99:
78 case dwarf::DW_LANG_C:
79 case dwarf::DW_LANG_C_plus_plus:
80 case dwarf::DW_LANG_ObjC:
81 case dwarf::DW_LANG_ObjC_plus_plus:
82 return 0;
83
84 case dwarf::DW_LANG_Fortran77:
85 case dwarf::DW_LANG_Fortran90:
86 case dwarf::DW_LANG_Fortran95:
87 return 1;
88
89 // The languages below have valid values only if the DWARF version >= 4.
90 case dwarf::DW_LANG_Java:
91 case dwarf::DW_LANG_Python:
92 case dwarf::DW_LANG_UPC:
93 case dwarf::DW_LANG_D:
94 if (dwarf::DWARF_VERSION >= 4)
95 return 0;
96 break;
97
98 case dwarf::DW_LANG_Ada83:
99 case dwarf::DW_LANG_Ada95:
100 case dwarf::DW_LANG_Cobol74:
101 case dwarf::DW_LANG_Cobol85:
102 case dwarf::DW_LANG_Modula2:
103 case dwarf::DW_LANG_Pascal83:
104 case dwarf::DW_LANG_PLI:
105 if (dwarf::DWARF_VERSION >= 4)
106 return 1;
107 break;
108 }
109
110 return -1;
111}
112
Manman Ren4dbdc902013-10-31 17:54:35 +0000113/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000114static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000115 // When the MDNode can be part of the type system, the DIE can be shared
116 // across CUs.
117 // Combining type units and cross-CU DIE sharing is lower value (since
118 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
119 // level already) but may be implementable for some value in projects
120 // building multiple independent libraries with LTO and then linking those
121 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000122 return (D.isType() ||
123 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
124 !GenerateTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000125}
126
127/// getDIE - Returns the debug information entry map slot for the
128/// specified debug variable. We delegate the request to DwarfDebug
129/// when the DIE for this MDNode can be shared across CUs. The mappings
130/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie2ad00162013-11-15 23:09:13 +0000131DIE *CompileUnit::getDIE(DIDescriptor D) const {
132 if (isShareableAcrossCUs(D))
133 return DD->getDIE(D);
134 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000135}
136
137/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
138/// when the DIE for this MDNode can be shared across CUs. The mappings
139/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie2ad00162013-11-15 23:09:13 +0000140void CompileUnit::insertDIE(DIDescriptor Desc, DIE *D) {
141 if (isShareableAcrossCUs(Desc)) {
142 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000143 return;
144 }
David Blaikie2ad00162013-11-15 23:09:13 +0000145 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000146}
147
Eric Christopherbb69a272012-08-24 01:14:27 +0000148/// addFlag - Add a flag that is true.
David Blaikief2443192013-10-21 17:28:37 +0000149void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000150 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000151 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000152 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000153 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000154}
155
Devang Patel0e821f42011-04-12 23:21:44 +0000156/// addUInt - Add an unsigned integer attribute data and value.
157///
David Blaikief2443192013-10-21 17:28:37 +0000158void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
159 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000160 if (!Form)
161 Form = DIEInteger::BestForm(false, Integer);
162 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
163 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000164 Die->addValue(Attribute, *Form, Value);
165}
166
167void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
168 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000169}
170
171/// addSInt - Add an signed integer attribute data and value.
172///
David Blaikief2443192013-10-21 17:28:37 +0000173void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
174 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000175 if (!Form)
176 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000177 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000178 Die->addValue(Attribute, *Form, Value);
179}
180
181void CompileUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
182 int64_t Integer) {
183 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000184}
185
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000186/// addString - Add a string attribute data and value. We always emit a
187/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000188/// more predictable sizes. In the case of split dwarf we emit an index
189/// into another table which gets us the static offset into the string
190/// table.
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000191void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute,
192 StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000193 DIEValue *Value;
David Blaikief2443192013-10-21 17:28:37 +0000194 dwarf::Form Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000195 if (!DD->useSplitDwarf()) {
196 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000197 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000198 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000199 else {
200 MCSymbol *StringPool = DU->getStringPoolSym();
201 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
202 }
Eric Christopher67646432013-07-26 17:02:41 +0000203 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000204 } else {
205 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000206 Value = new (DIEValueAllocator) DIEInteger(idx);
207 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000208 }
Eric Christopher67646432013-07-26 17:02:41 +0000209 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
210 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000211}
212
213/// addLocalString - Add a string attribute data and value. This is guaranteed
214/// to be in the local string pool instead of indirected.
David Blaikief2443192013-10-21 17:28:37 +0000215void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher2cbd5762013-01-07 19:32:41 +0000216 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000217 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000218 DIEValue *Value;
219 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000220 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 else {
Eric Christophere698f532012-12-20 21:58:36 +0000222 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000223 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000224 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000225 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000226}
227
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000228/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000229///
David Blaikief2443192013-10-21 17:28:37 +0000230void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000231 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000232 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000233}
234
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235/// addLabel - Add a Dwarf label attribute data and value.
236///
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000237void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute,
238 dwarf::Form Form, const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000239 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
240 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000241}
242
David Blaikief2443192013-10-21 17:28:37 +0000243void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
244 const MCSymbol *Label) {
245 addLabel(Die, (dwarf::Attribute)0, Form, Label);
246}
247
Eric Christopher962c9082013-01-15 23:56:56 +0000248/// addLabelAddress - Add a dwarf label attribute data and value using
249/// DW_FORM_addr or DW_FORM_GNU_addr_index.
250///
David Blaikief2443192013-10-21 17:28:37 +0000251void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000252 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000253 if (Label)
254 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000255
Eric Christopher962c9082013-01-15 23:56:56 +0000256 if (!DD->useSplitDwarf()) {
257 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000258 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000259 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
260 } else {
261 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
262 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
263 }
264 } else {
265 unsigned idx = DU->getAddrPoolIndex(Label);
266 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
267 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
268 }
269}
270
Eric Christophere9ec2452013-01-18 22:11:33 +0000271/// addOpAddress - Add a dwarf op address data and value using the
272/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
273///
David Blaikief2443192013-10-21 17:28:37 +0000274void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000275 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000276 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000277 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
278 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000279 } else {
David Blaikief2443192013-10-21 17:28:37 +0000280 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
281 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000282 }
283}
284
Devang Patel0e821f42011-04-12 23:21:44 +0000285/// addDelta - Add a label delta attribute data and value.
286///
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000287void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute,
288 dwarf::Form Form, const MCSymbol *Hi,
289 const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000290 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
291 Die->addValue(Attribute, Form, Value);
292}
293
294/// addDIEEntry - Add a DIE attribute data and value.
295///
Eric Christopher98b7f172013-11-11 18:52:36 +0000296void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
297 DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000298 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
299}
300
301void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
302 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000303 const DIE *DieCU = Die->getUnitOrNull();
304 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000305 if (!DieCU)
306 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
307 DieCU = getCUDie();
308 if (!EntryCU)
309 EntryCU = getCUDie();
310 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
311 : dwarf::DW_FORM_ref_addr,
312 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000313}
314
Manman Renb987e512013-10-29 00:53:03 +0000315/// Create a DIE with the given Tag, add the DIE to its parent, and
316/// call insertDIE if MD is not null.
David Blaikie52c50202013-11-16 00:29:01 +0000317DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000318 DIE *Die = new DIE(Tag);
319 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000320 if (N)
321 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000322 return Die;
323}
324
Devang Patel0e821f42011-04-12 23:21:44 +0000325/// addBlock - Add block data.
326///
David Blaikief2443192013-10-21 17:28:37 +0000327void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000328 DIEBlock *Block) {
329 Block->ComputeSize(Asm);
330 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
331 Die->addValue(Attribute, Block->BestForm(), Block);
332}
333
334/// addSourceLine - Add location information to specified debug information
335/// entry.
336void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
337 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000338 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000339 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000340
Devang Patel0e821f42011-04-12 23:21:44 +0000341 unsigned Line = V.getLineNumber();
342 if (Line == 0)
343 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000344 unsigned FileID =
345 DD->getOrCreateSourceID(V.getContext().getFilename(),
346 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000347 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000348 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
349 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000350}
351
352/// addSourceLine - Add location information to specified debug information
353/// entry.
354void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
355 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000356 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000357 return;
358
359 unsigned Line = G.getLineNumber();
360 if (Line == 0)
361 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000362 unsigned FileID =
363 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000364 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000365 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
366 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000367}
368
369/// addSourceLine - Add location information to specified debug information
370/// entry.
371void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
372 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000373 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000374 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000375
Devang Patel0e821f42011-04-12 23:21:44 +0000376 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000377 unsigned Line = SP.getLineNumber();
378 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000379 return;
380
Eric Christopherc2697f82013-10-19 01:04:47 +0000381 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
382 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000383 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000384 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
385 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000386}
387
388/// addSourceLine - Add location information to specified debug information
389/// entry.
390void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
391 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000392 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000393 return;
394
395 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000396 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000397 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000398 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
399 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000400 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000401 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
402 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000403}
404
405/// addSourceLine - Add location information to specified debug information
406/// entry.
Eric Christopher70e1bd82012-03-29 08:42:56 +0000407void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
408 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000409 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000410 return;
411
412 unsigned Line = Ty.getLineNumber();
413 if (Line == 0)
414 return;
415 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000416 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000417 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000418 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000419 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
420 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000421}
422
423/// addSourceLine - Add location information to specified debug information
424/// entry.
Devang Patel0e821f42011-04-12 23:21:44 +0000425void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
426 // Verify namespace.
427 if (!NS.Verify())
428 return;
429
430 unsigned Line = NS.getLineNumber();
431 if (Line == 0)
432 return;
433 StringRef FN = NS.getFilename();
434
Eric Christopherc2697f82013-10-19 01:04:47 +0000435 unsigned FileID =
436 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000437 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000438 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
439 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000440}
441
Eric Christopher92331fd2012-11-21 00:34:38 +0000442/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000443/// DbgVariable based on provided MachineLocation.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000444void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patel77dc5412011-04-27 22:45:24 +0000445 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000446 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000447 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000448 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000449 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
450 else
David Blaikieea2605d2013-06-20 00:25:24 +0000451 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000452 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000453}
454
Devang Patelba5fbf12011-04-26 19:06:18 +0000455/// addRegisterOp - Add register operand.
David Blaikief2443192013-10-21 17:28:37 +0000456void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000457 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
458 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
459 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000460 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000461 else {
David Blaikief2443192013-10-21 17:28:37 +0000462 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
463 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000464 }
465}
466
467/// addRegisterOffset - Add register offset.
David Blaikief2443192013-10-21 17:28:37 +0000468void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
469 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000470 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
471 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
472 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
473 if (Reg == TRI->getFrameRegister(*Asm->MF))
474 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000475 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000476 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000477 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000478 else {
David Blaikief2443192013-10-21 17:28:37 +0000479 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
480 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000481 }
David Blaikief2443192013-10-21 17:28:37 +0000482 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000483}
484
485/// addAddress - Add an address attribute to a die based on the location
486/// provided.
David Blaikief2443192013-10-21 17:28:37 +0000487void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
David Blaikieea2605d2013-06-20 00:25:24 +0000488 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000489 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
490
David Blaikieea2605d2013-06-20 00:25:24 +0000491 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000492 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000493 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000494 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000495 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000496 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000497 }
498 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000499
500 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000501 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000502}
503
Devang Patel0e821f42011-04-12 23:21:44 +0000504/// addComplexAddress - Start with the address based on the location provided,
505/// and generate the DWARF information necessary to find the actual variable
506/// given the extra address information encoded in the DIVariable, starting from
507/// the starting location. Add the DWARF information to the die.
508///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000509void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
David Blaikief2443192013-10-21 17:28:37 +0000510 dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000511 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000512 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000513 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000514 unsigned i = 0;
515 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000516 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000517 // If first address element is OpPlus then emit
518 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000519 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000520 i = 2;
521 } else
522 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000523 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000524 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000525
Eric Christopherc2697f82013-10-19 01:04:47 +0000526 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000527 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000528 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000529 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
530 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000531 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000532 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000533 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000534 } else
535 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000536 }
537
538 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000539 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000540}
541
542/* Byref variables, in Blocks, are declared by the programmer as "SomeType
543 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
544 gives the variable VarName either the struct, or a pointer to the struct, as
545 its type. This is necessary for various behind-the-scenes things the
546 compiler needs to do with by-reference variables in Blocks.
547
548 However, as far as the original *programmer* is concerned, the variable
549 should still have type 'SomeType', as originally declared.
550
551 The function getBlockByrefType dives into the __Block_byref_x_VarName
552 struct to find the original type of the variable, which is then assigned to
553 the variable's Debug Information Entry as its real type. So far, so good.
554 However now the debugger will expect the variable VarName to have the type
555 SomeType. So we need the location attribute for the variable to be an
556 expression that explains to the debugger how to navigate through the
557 pointers and struct to find the actual variable of type SomeType.
558
559 The following function does just that. We start by getting
560 the "normal" location for the variable. This will be the location
561 of either the struct __Block_byref_x_VarName or the pointer to the
562 struct __Block_byref_x_VarName.
563
564 The struct will look something like:
565
566 struct __Block_byref_x_VarName {
567 ... <various fields>
568 struct __Block_byref_x_VarName *forwarding;
569 ... <various other fields>
570 SomeType VarName;
571 ... <maybe more fields>
572 };
573
574 If we are given the struct directly (as our starting point) we
575 need to tell the debugger to:
576
577 1). Add the offset of the forwarding field.
578
579 2). Follow that pointer to get the real __Block_byref_x_VarName
580 struct to use (the real one may have been copied onto the heap).
581
582 3). Add the offset for the field VarName, to find the actual variable.
583
584 If we started with a pointer to the struct, then we need to
585 dereference that pointer first, before the other steps.
586 Translating this into DWARF ops, we will need to append the following
587 to the current location description for the variable:
588
589 DW_OP_deref -- optional, if we start with a pointer
590 DW_OP_plus_uconst <forward_fld_offset>
591 DW_OP_deref
592 DW_OP_plus_uconst <varName_fld_offset>
593
594 That is what this function does. */
595
596/// addBlockByrefAddress - Start with the address based on the location
597/// provided, and generate the DWARF information necessary to find the
598/// actual Block variable (navigating the Block struct) based on the
599/// starting location. Add the DWARF information to the die. For
600/// more information, read large comment just above here.
601///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000602void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
David Blaikief2443192013-10-21 17:28:37 +0000603 dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000604 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000605 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000606 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000607 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000608 bool isPointer = false;
609
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000610 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000611
612 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000613 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000614 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000615 isPointer = true;
616 }
617
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000618 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000619
620 // Find the __forwarding field and the variable field in the __Block_byref
621 // struct.
622 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000623 DIDerivedType varField;
624 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000625
626 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000627 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000628 StringRef fieldName = DT.getName();
629 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000630 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000631 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000632 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000633 }
634
635 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000636 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
637 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000638
639 // Decode the original location, and use that as the start of the byref
640 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000641 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
642
Eric Christopheref9d7102012-07-04 02:02:18 +0000643 if (Location.isReg())
644 addRegisterOp(Block, Location.getReg());
645 else
646 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000647
648 // If we started with a pointer to the __Block_byref... struct, then
649 // the first thing we need to do is dereference the pointer (DW_OP_deref).
650 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000651 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000652
653 // Next add the offset for the '__forwarding' field:
654 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
655 // adding the offset if it's 0.
656 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000657 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
658 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000659 }
660
661 // Now dereference the __forwarding field to get to the real __Block_byref
662 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000663 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000664
665 // Now that we've got the real __Block_byref... struct, add the offset
666 // for the variable's field to get to the location of the actual variable:
667 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
668 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000669 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
670 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000671 }
672
673 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000674 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000675}
676
Devang Patelbcd50a12011-07-20 21:57:04 +0000677/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000678static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000679 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000680 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
681 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000682 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000683 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
684 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000685 *SizeInBits = Ty.getSizeInBits();
686 return true;
687 }
688 return false;
689}
690
Manman Renb3388602013-10-05 01:43:03 +0000691/// Return true if type encoding is unsigned.
692static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
693 DIDerivedType DTy(Ty);
694 if (DTy.isDerivedType())
695 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
696
697 DIBasicType BTy(Ty);
698 if (BTy.isBasicType()) {
699 unsigned Encoding = BTy.getEncoding();
700 if (Encoding == dwarf::DW_ATE_unsigned ||
701 Encoding == dwarf::DW_ATE_unsigned_char ||
702 Encoding == dwarf::DW_ATE_boolean)
703 return true;
704 }
705 return false;
706}
707
708/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000709static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000710 unsigned Tag = Ty.getTag();
711
712 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
713 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
714 Tag != dwarf::DW_TAG_restrict_type)
715 return Ty.getSizeInBits();
716
717 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
718
719 // If this type is not derived from any type then take conservative approach.
720 if (!BaseType.isValid())
721 return Ty.getSizeInBits();
722
723 // If this is a derived type, go ahead and get the base type, unless it's a
724 // reference then it's just the size of the field. Pointer types have no need
725 // of this since they're a different type of qualification on the type.
726 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
727 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
728 return Ty.getSizeInBits();
729
730 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000731 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000732
733 return BaseType.getSizeInBits();
734}
735
Devang Patel0e821f42011-04-12 23:21:44 +0000736/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000737void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
Devang Patel3c6aed22011-05-27 16:45:18 +0000738 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000739 // FIXME: This is a bit conservative/simple - it emits negative values at
740 // their maximum bit width which is a bit unfortunate (& doesn't prefer
741 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000742 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000743 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000744 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000745 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000746
Eric Christopher9d1daa82013-08-27 23:49:04 +0000747 // If we're a signed constant definitely use sdata.
748 if (SignedConstant) {
749 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
750 return;
751 }
752
753 // Else use data for now unless it's larger than we can deal with.
754 switch (SizeInBits) {
755 case 8:
756 Form = dwarf::DW_FORM_data1;
757 break;
758 case 16:
759 Form = dwarf::DW_FORM_data2;
760 break;
761 case 32:
762 Form = dwarf::DW_FORM_data4;
763 break;
764 case 64:
765 Form = dwarf::DW_FORM_data8;
766 break;
767 default:
768 Form = dwarf::DW_FORM_udata;
769 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
770 return;
771 }
772 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000773}
774
775/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000776void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000777 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000778 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
779 APFloat FPImm = MO.getFPImm()->getValueAPF();
780
781 // Get the raw data form of the floating point.
782 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000783 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000784
785 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000786 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000787 int Incr = (LittleEndian ? 1 : -1);
788 int Start = (LittleEndian ? 0 : NumBytes - 1);
789 int Stop = (LittleEndian ? NumBytes : -1);
790
791 // Output the constant to DWARF one byte at a time.
792 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000793 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000794
David Blaikief2443192013-10-21 17:28:37 +0000795 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000796}
797
David Blaikiea39a76e2013-01-20 01:18:01 +0000798/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000799void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000800 // Pass this down to addConstantValue as an unsigned bag of bits.
801 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000802}
803
Devang Patel0e821f42011-04-12 23:21:44 +0000804/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000805void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel0e821f42011-04-12 23:21:44 +0000806 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000807 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000808}
809
810// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000811void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000812 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000813 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000814 // If we're a signed constant definitely use sdata.
815 if (!Unsigned) {
816 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
817 Val.getSExtValue());
818 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000819 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000820
821 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000822 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000823 switch (CIBitWidth) {
824 case 8:
825 Form = dwarf::DW_FORM_data1;
826 break;
827 case 16:
828 Form = dwarf::DW_FORM_data2;
829 break;
830 case 32:
831 Form = dwarf::DW_FORM_data4;
832 break;
833 case 64:
834 Form = dwarf::DW_FORM_data8;
835 break;
836 default:
837 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
838 Val.getZExtValue());
839 return;
840 }
841 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000842 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000843 }
844
845 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
846
847 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000848 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000849
850 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000851 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000852
853 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000854 for (int i = 0; i < NumBytes; i++) {
855 uint8_t c;
856 if (LittleEndian)
857 c = Ptr64[i / 8] >> (8 * (i & 7));
858 else
859 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000860 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000861 }
Devang Patel0e821f42011-04-12 23:21:44 +0000862
David Blaikief2443192013-10-21 17:28:37 +0000863 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000864}
865
Eric Christopher25e35092013-04-22 07:47:40 +0000866/// addTemplateParams - Add template parameters into buffer.
Devang Patel0e821f42011-04-12 23:21:44 +0000867void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
868 // Add template parameters.
869 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
870 DIDescriptor Element = TParams.getElement(i);
871 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000872 constructTemplateTypeParameterDIE(Buffer,
873 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000874 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000875 constructTemplateValueParameterDIE(Buffer,
876 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000877 }
Devang Patel0e821f42011-04-12 23:21:44 +0000878}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000879
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000880/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christopher08f7c8f2013-10-04 23:49:26 +0000881DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000882 if (!Context || Context.isFile())
883 return getCUDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000884 if (Context.isType())
885 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000886 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000887 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000888 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000889 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000890 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000891}
892
David Blaikie409dd9c2013-11-19 23:08:21 +0000893DIE *CompileUnit::createTypeDIE(DICompositeType Ty) {
894 DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext()));
895
896 DIE *TyDIE = getDIE(Ty);
897 if (TyDIE)
898 return TyDIE;
899
900 // Create new type.
901 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
902
903 constructTypeDIEImpl(*TyDIE, Ty);
904
905 updateAcceleratorTables(Ty, TyDIE);
906 return TyDIE;
907}
908
Devang Patel0e821f42011-04-12 23:21:44 +0000909/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
910/// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000911DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000912 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000913 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000914
David Blaikie32887552013-11-14 22:25:02 +0000915 DIType Ty(TyNode);
916 assert(Ty.isType());
917
Manman Renf4c339e2013-10-29 22:49:29 +0000918 // Construct the context before querying for the existence of the DIE in case
919 // such construction creates the DIE.
920 DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext()));
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000921 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000922
Eric Christophere595bae2013-10-04 17:08:38 +0000923 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000924 if (TyDIE)
925 return TyDIE;
926
927 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000928 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
929
Devang Patel0e821f42011-04-12 23:21:44 +0000930 if (Ty.isBasicType())
931 constructTypeDIE(*TyDIE, DIBasicType(Ty));
932 else if (Ty.isCompositeType())
933 constructTypeDIE(*TyDIE, DICompositeType(Ty));
934 else {
935 assert(Ty.isDerivedType() && "Unknown kind of DIType");
936 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
937 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000938
939 updateAcceleratorTables(Ty, TyDIE);
940
941 return TyDIE;
942}
943
944void CompileUnit::updateAcceleratorTables(DIType Ty, const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000945 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
946 bool IsImplementation = 0;
947 if (Ty.isCompositeType()) {
948 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000949 // A runtime language of 0 actually means C/C++ and that any
950 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +0000951 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000952 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000953 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000954 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopher21bde872012-01-06 04:35:23 +0000955 }
Devang Patel0e821f42011-04-12 23:21:44 +0000956}
957
958/// addType - Add a new type attribute to the specified entity.
David Blaikief2443192013-10-21 17:28:37 +0000959void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000960 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000961
962 // Check for pre-existence.
963 DIEEntry *Entry = getDIEEntry(Ty);
964 // If it exists then use the existing value.
965 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000966 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000967 return;
968 }
969
970 // Construct type.
971 DIE *Buffer = getOrCreateTypeDIE(Ty);
972
973 // Set up proxy.
974 Entry = createDIEEntry(Buffer);
975 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +0000976 addDIEEntry(Entity, Attribute, Entry);
Devang Patele9853f22011-05-31 22:56:51 +0000977
978 // If this is a complete composite type then include it in the
979 // list of global types.
Devang Patel562c7422011-06-01 00:23:24 +0000980 addGlobalType(Ty);
Devang Patel1cb8ab42011-05-31 23:30:30 +0000981}
982
Eric Christopher9cd26af2013-09-20 23:22:52 +0000983// Accelerator table mutators - add each name along with its companion
984// DIE to the proper table while ensuring that the name that we're going
985// to reference is in the string table. We do this since the names we
986// add may not only be identical to the names in the DIE.
David Blaikie2ea848b2013-11-19 22:51:04 +0000987void CompileUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +0000988 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +0000989 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000990 DIEs.push_back(Die);
991}
992
David Blaikie2ea848b2013-11-19 22:51:04 +0000993void CompileUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +0000994 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +0000995 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000996 DIEs.push_back(Die);
997}
998
David Blaikie2ea848b2013-11-19 22:51:04 +0000999void CompileUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001000 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001001 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001002 DIEs.push_back(Die);
1003}
1004
David Blaikie2ea848b2013-11-19 22:51:04 +00001005void CompileUnit::addAccelType(StringRef Name,
1006 std::pair<const DIE *, unsigned> Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001007 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001008 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001009 DIEs.push_back(Die);
1010}
1011
Eric Christopher9c58f312013-09-20 22:20:55 +00001012/// addGlobalName - Add a new global name to the compile unit.
Eric Christopher2c8b7902013-10-17 02:06:06 +00001013void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001014 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001015 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001016}
1017
Devang Patel1cb8ab42011-05-31 23:30:30 +00001018/// addGlobalType - Add a new global type to the compile unit.
1019///
Devang Patel562c7422011-06-01 00:23:24 +00001020void CompileUnit::addGlobalType(DIType Ty) {
Eric Christopher9e429ae2013-10-05 00:27:02 +00001021 DIScope Context = resolve(Ty.getContext());
Eric Christopher6d0f1e62013-09-23 23:15:58 +00001022 if (!Ty.getName().empty() && !Ty.isForwardDecl() &&
Eric Christopherb0fc0b92013-09-23 22:59:11 +00001023 (!Context || Context.isCompileUnit() || Context.isFile() ||
1024 Context.isNameSpace()))
Eric Christopher2c8b7902013-10-17 02:06:06 +00001025 if (DIEEntry *Entry = getDIEEntry(Ty)) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001026 std::string FullName =
1027 getParentContextString(Context) + Ty.getName().str();
Eric Christopherc2697f82013-10-19 01:04:47 +00001028 GlobalTypes[FullName] = Entry->getEntry();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001029 }
1030}
1031
1032/// getParentContextString - Walks the metadata parent chain in a language
1033/// specific manner (using the compile unit language) and returns
1034/// it as a string. This is done at the metadata level because DIEs may
1035/// not currently have been added to the parent context and walking the
1036/// DIEs looking for names is more expensive than walking the metadata.
1037std::string CompileUnit::getParentContextString(DIScope Context) const {
1038 if (!Context)
1039 return "";
1040
1041 // FIXME: Decide whether to implement this for non-C++ languages.
1042 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1043 return "";
1044
Eric Christopher8dba0d52013-10-19 01:04:42 +00001045 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001046 SmallVector<DIScope, 1> Parents;
1047 while (!Context.isCompileUnit()) {
1048 Parents.push_back(Context);
1049 if (Context.getContext())
1050 Context = resolve(Context.getContext());
1051 else
1052 // Structure, etc types will have a NULL context if they're at the top
1053 // level.
1054 break;
1055 }
1056
1057 // Reverse iterate over our list to go from the outermost construct to the
1058 // innermost.
1059 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1060 E = Parents.rend();
1061 I != E; ++I) {
1062 DIScope Ctx = *I;
1063 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001064 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001065 CS += Name;
1066 CS += "::";
1067 }
1068 }
1069 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001070}
1071
Eric Christopherfa205ca2013-10-05 00:05:51 +00001072/// addPubTypes - Add subprogram argument types for pubtypes section.
Devang Patel17b53272011-05-06 16:57:54 +00001073void CompileUnit::addPubTypes(DISubprogram SP) {
1074 DICompositeType SPTy = SP.getType();
Eric Christopher31b05762013-08-08 01:41:00 +00001075 uint16_t SPTag = SPTy.getTag();
Devang Patel17b53272011-05-06 16:57:54 +00001076 if (SPTag != dwarf::DW_TAG_subroutine_type)
1077 return;
1078
1079 DIArray Args = SPTy.getTypeArray();
1080 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1081 DIType ATy(Args.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001082 if (!ATy.isType())
Devang Patel17b53272011-05-06 16:57:54 +00001083 continue;
Devang Patel562c7422011-06-01 00:23:24 +00001084 addGlobalType(ATy);
Devang Patel17b53272011-05-06 16:57:54 +00001085 }
1086}
1087
Devang Patel0e821f42011-04-12 23:21:44 +00001088/// constructTypeDIE - Construct basic type die from DIBasicType.
1089void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1090 // Get core information.
1091 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001092 // Add name if not anonymous or intermediate type.
1093 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001094 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001095
David Blaikiefac56122013-10-04 23:21:16 +00001096 // An unspecified type only has a name attribute.
1097 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001098 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001099
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001100 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001101 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001102
Devang Patel0e821f42011-04-12 23:21:44 +00001103 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001104 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001105}
1106
1107/// constructTypeDIE - Construct derived type die from DIDerivedType.
1108void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1109 // Get core information.
1110 StringRef Name = DTy.getName();
1111 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001112 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001113
1114 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001115 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001116 if (FromTy)
1117 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001118
1119 // Add name if not anonymous or intermediate type.
1120 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001121 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001122
1123 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001124 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001125 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001126
David Blaikie5d3249b2013-01-07 05:51:15 +00001127 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001128 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1129 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001130 // Add source line info if available and TyDesc is not a forward declaration.
1131 if (!DTy.isForwardDecl())
1132 addSourceLine(&Buffer, DTy);
1133}
1134
Eric Christopher67646432013-07-26 17:02:41 +00001135/// Return true if the type is appropriately scoped to be contained inside
1136/// its own type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001137static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
Manman Ren116868e2013-09-09 19:47:11 +00001138 DIScope Parent = DD->resolve(Ty.getContext());
Eric Christopher67646432013-07-26 17:02:41 +00001139 while (Parent) {
1140 // Don't generate a hash for anything scoped inside a function.
1141 if (Parent.isSubprogram())
1142 return false;
Manman Rende897a362013-09-09 22:35:23 +00001143 Parent = DD->resolve(Parent.getContext());
Eric Christopher67646432013-07-26 17:02:41 +00001144 }
1145 return true;
1146}
1147
1148/// Return true if the type should be split out into a type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001149static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001150 if (!GenerateTypeUnits)
1151 return false;
1152
Eric Christopher31b05762013-08-08 01:41:00 +00001153 uint16_t Tag = CTy.getTag();
Eric Christopher67646432013-07-26 17:02:41 +00001154
1155 switch (Tag) {
1156 case dwarf::DW_TAG_structure_type:
1157 case dwarf::DW_TAG_union_type:
1158 case dwarf::DW_TAG_enumeration_type:
1159 case dwarf::DW_TAG_class_type:
1160 // If this is a class, structure, union, or enumeration type
David Blaikie93ff1eb2013-10-04 23:52:02 +00001161 // that is a definition (not a declaration), and not scoped
Eric Christopher67646432013-07-26 17:02:41 +00001162 // inside a function then separate this out as a type unit.
David Blaikie93ff1eb2013-10-04 23:52:02 +00001163 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
Eric Christopher67646432013-07-26 17:02:41 +00001164 default:
David Blaikie93ff1eb2013-10-04 23:52:02 +00001165 return false;
Eric Christopher67646432013-07-26 17:02:41 +00001166 }
1167}
1168
Devang Patel0e821f42011-04-12 23:21:44 +00001169/// constructTypeDIE - Construct type DIE from DICompositeType.
1170void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001171 // If this is a type applicable to a type unit it then add it to the
1172 // list of types we'll compute a hash for later.
1173 if (shouldCreateTypeUnit(CTy, DD))
1174 DD->addTypeUnitType(&Buffer, CTy);
1175 else
1176 constructTypeDIEImpl(Buffer, CTy);
1177}
1178
1179void CompileUnit::constructTypeDIEImpl(DIE &Buffer, DICompositeType CTy) {
1180 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001181 StringRef Name = CTy.getName();
1182
1183 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001184 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001185
1186 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001187 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001188 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001189 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001190 case dwarf::DW_TAG_enumeration_type:
1191 constructEnumTypeDIE(Buffer, CTy);
1192 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001193 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001194 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001195 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001196 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001197 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001198 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001199
1200 bool isPrototyped = true;
1201 // Add arguments.
1202 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1203 DIDescriptor Ty = Elements.getElement(i);
1204 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001205 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001206 isPrototyped = false;
1207 } else {
Manman Renb987e512013-10-29 00:53:03 +00001208 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001209 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001210 if (DIType(Ty).isArtificial())
1211 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001212 }
1213 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001214 // Add prototype flag if we're dealing with a C language and the
1215 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001216 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001217 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001218 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001219 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001220 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Eric Christopherc2697f82013-10-19 01:04:47 +00001221 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001222 case dwarf::DW_TAG_structure_type:
1223 case dwarf::DW_TAG_union_type:
1224 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001225 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001226 DIArray Elements = CTy.getTypeArray();
1227 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001228 DIDescriptor Element = Elements.getElement(i);
1229 DIE *ElemDie = NULL;
1230 if (Element.isSubprogram()) {
1231 DISubprogram SP(Element);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001232 ElemDie = getOrCreateSubprogramDIE(SP);
Devang Patel0e821f42011-04-12 23:21:44 +00001233 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001234 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001235 dwarf::DW_ACCESS_protected);
1236 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001237 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001238 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001239 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001240 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001241 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001242 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001243 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001244 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001245 DIDerivedType DDTy(Element);
1246 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001247 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001248 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001249 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001250 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001251 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001252 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001253 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001254 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001255 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001256 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001257 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001258 StringRef PropertyName = Property.getObjCPropertyName();
1259 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001260 addType(ElemDie, Property.getType());
1261 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001262 StringRef GetterName = Property.getObjCPropertyGetterName();
1263 if (!GetterName.empty())
1264 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1265 StringRef SetterName = Property.getObjCPropertySetterName();
1266 if (!SetterName.empty())
1267 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1268 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001269 if (Property.isReadOnlyObjCProperty())
1270 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1271 if (Property.isReadWriteObjCProperty())
1272 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1273 if (Property.isAssignObjCProperty())
1274 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1275 if (Property.isRetainObjCProperty())
1276 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1277 if (Property.isCopyObjCProperty())
1278 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1279 if (Property.isNonAtomicObjCProperty())
1280 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1281 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001282 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001283 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001284
Devang Pateld925d1a2012-02-07 23:33:58 +00001285 DIEEntry *Entry = getDIEEntry(Element);
1286 if (!Entry) {
1287 Entry = createDIEEntry(ElemDie);
1288 insertDIEEntry(Element, Entry);
1289 }
Devang Patel403e8192012-02-04 01:30:32 +00001290 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001291 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001292 }
1293
1294 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001295 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001296
Eric Christopher9e429ae2013-10-05 00:27:02 +00001297 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001298 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001299 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001300 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001301
Devang Patel12419ae2011-05-12 21:29:42 +00001302 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001303 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001304
Eric Christopherda011dd2011-12-16 23:42:42 +00001305 // Add template parameters to a class, structure or union types.
1306 // FIXME: The support isn't in the metadata for this yet.
1307 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001308 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001309 addTemplateParams(Buffer, CTy.getTemplateParams());
1310
1311 break;
1312 }
1313 default:
1314 break;
1315 }
1316
1317 // Add name if not anonymous or intermediate type.
1318 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001319 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001320
Eric Christopher775cbd22012-05-22 18:45:18 +00001321 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001322 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001323 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001324 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001325 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001326 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001327 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001328 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001329 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001330 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001331
1332 // If we're a forward decl, say so.
1333 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001334 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001335
1336 // Add source line info if available.
1337 if (!CTy.isForwardDecl())
1338 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001339
1340 // No harm in adding the runtime language to the declaration.
1341 unsigned RLang = CTy.getRunTimeLang();
1342 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001343 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1344 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001345 }
1346}
1347
Manman Renffc9a712013-10-23 23:05:28 +00001348/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1349/// DITemplateTypeParameter.
Manman Ren57e6ff72013-10-23 22:57:12 +00001350void
Manman Renffc9a712013-10-23 23:05:28 +00001351CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1352 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001353 DIE *ParamDIE =
1354 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001355 // Add the type if it exists, it could be void and therefore no type.
1356 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001357 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001358 if (!TP.getName().empty())
1359 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001360}
1361
Manman Renffc9a712013-10-23 23:05:28 +00001362/// constructTemplateValueParameterDIE - Construct new DIE for the given
1363/// DITemplateValueParameter.
Manman Ren57e6ff72013-10-23 22:57:12 +00001364void
Manman Renffc9a712013-10-23 23:05:28 +00001365CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1366 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001367 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001368
1369 // Add the type if there is one, template template and template parameter
1370 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001371 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001372 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001373 if (!VP.getName().empty())
1374 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1375 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001376 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001377 addConstantValue(ParamDIE, CI,
1378 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001379 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1380 // For declaration non-type template parameters (such as global values and
1381 // functions)
1382 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001383 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001384 // Emit DW_OP_stack_value to use the address as the immediate value of the
1385 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001386 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1387 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001388 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001389 assert(isa<MDString>(Val));
1390 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1391 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001392 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001393 assert(isa<MDNode>(Val));
1394 DIArray A(cast<MDNode>(Val));
1395 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001396 }
1397 }
Devang Patel0e821f42011-04-12 23:21:44 +00001398}
1399
Devang Patel17b53272011-05-06 16:57:54 +00001400/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1401DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001402 // Construct the context before querying for the existence of the DIE in case
1403 // such construction creates the DIE.
1404 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001405
Devang Patel17b53272011-05-06 16:57:54 +00001406 DIE *NDie = getDIE(NS);
1407 if (NDie)
1408 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001409 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1410
Eric Christopher4996c702011-11-07 09:24:32 +00001411 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001412 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001413 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001414 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001415 } else
1416 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001417 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001418 return NDie;
1419}
1420
Devang Patel89543712011-08-15 17:24:54 +00001421/// getOrCreateSubprogramDIE - Create new DIE using SP.
1422DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001423 // Construct the context before querying for the existence of the DIE in case
1424 // such construction creates the DIE (as is the case for member function
1425 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001426 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001427
Eric Christophere595bae2013-10-04 17:08:38 +00001428 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001429 if (SPDie)
1430 return SPDie;
1431
Manman Ren73d697c2013-10-29 00:58:04 +00001432 DISubprogram SPDecl = SP.getFunctionDeclaration();
1433 if (SPDecl.isSubprogram())
1434 // Add subprogram definitions to the CU die directly.
1435 ContextDIE = CUDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001436
1437 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001438 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001439
Rafael Espindola79278362011-11-10 22:34:29 +00001440 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001441 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001442 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001443
Devang Patel89543712011-08-15 17:24:54 +00001444 // Add function template parameters.
1445 addTemplateParams(*SPDie, SP.getTemplateParams());
1446
Devang Patel89543712011-08-15 17:24:54 +00001447 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001448 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001449 if (DeclDie) {
1450 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001451 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001452
Devang Patel89543712011-08-15 17:24:54 +00001453 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001454 }
Devang Patel89543712011-08-15 17:24:54 +00001455
Eric Christopheracb71152012-08-23 22:52:55 +00001456 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001457 StringRef LinkageName = SP.getLinkageName();
1458 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001459 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001460 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001461
Devang Patel89543712011-08-15 17:24:54 +00001462 // Constructors and operators for anonymous aggregates do not have names.
1463 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001464 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001465
1466 addSourceLine(SPDie, SP);
1467
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001468 // Add the prototype if we have a prototype and we have a C like
1469 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001470 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001471 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001472 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001473 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001474 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001475
Devang Patel89543712011-08-15 17:24:54 +00001476 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001477 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1478 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001479
David Blaikie5174c842013-05-22 23:22:18 +00001480 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001481 // Add a return type. If this is a type like a C/C++ void type we don't add a
1482 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001483 if (Args.getElement(0))
1484 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001485
1486 unsigned VK = SP.getVirtuality();
1487 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001488 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001489 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001490 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1491 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1492 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001493 ContainingTypeMap.insert(
1494 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001495 }
1496
1497 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001498 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001499
Devang Patel89543712011-08-15 17:24:54 +00001500 // Add arguments. Do not add arguments for subprogram definition. They will
1501 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001502 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001503 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001504 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001505 addType(Arg, ATy);
1506 if (ATy.isArtificial())
1507 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001508 }
Devang Patel89543712011-08-15 17:24:54 +00001509 }
1510
1511 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001512 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001513
1514 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001515 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001516
1517 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001518 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001519
1520 if (unsigned isa = Asm->getISAEncoding()) {
1521 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1522 }
1523
1524 return SPDie;
1525}
1526
Devang Pateldfd6ec32011-08-15 17:57:41 +00001527// Return const expression if value is a GEP to access merged global
1528// constant. e.g.
1529// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1530static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1531 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1532 if (!CE || CE->getNumOperands() != 3 ||
1533 CE->getOpcode() != Instruction::GetElementPtr)
1534 return NULL;
1535
1536 // First operand points to a global struct.
1537 Value *Ptr = CE->getOperand(0);
1538 if (!isa<GlobalValue>(Ptr) ||
1539 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1540 return NULL;
1541
1542 // Second operand is zero.
1543 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1544 if (!CI || !CI->isZero())
1545 return NULL;
1546
1547 // Third operand is offset.
1548 if (!isa<ConstantInt>(CE->getOperand(2)))
1549 return NULL;
1550
1551 return CE;
1552}
1553
1554/// createGlobalVariableDIE - create global variable DIE.
David Blaikiea781b25b2013-11-17 21:55:13 +00001555void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001556 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001557 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001558 return;
1559
Manman Ren7504ed42013-07-08 18:33:29 +00001560 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001561 return;
1562
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001563 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001564 DIType GTy = GV.getType();
1565
1566 // If this is a static data member definition, some attributes belong
1567 // to the declaration DIE.
1568 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001569 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001570 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1571 if (SDMDecl.Verify()) {
1572 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1573 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001574 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001575 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001576 }
1577
1578 // If this is not a static data member definition, create the variable
1579 // DIE and add the initial set of attributes to it.
1580 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001581 // Construct the context before querying for the existence of the DIE in
1582 // case such construction creates the DIE.
1583 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001584
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001585 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001586 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001587
1588 // Add name and type.
1589 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1590 addType(VariableDIE, GTy);
1591
1592 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001593 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001594 addFlag(VariableDIE, dwarf::DW_AT_external);
1595
1596 // Add line number info.
1597 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001598 }
1599
Devang Pateldfd6ec32011-08-15 17:57:41 +00001600 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001601 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001602 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001603 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001604 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001605 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001606 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001607 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001608 if (GV.getGlobal()->isThreadLocal()) {
1609 // FIXME: Make this work with -gsplit-dwarf.
1610 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1611 assert((PointerSize == 4 || PointerSize == 8) &&
1612 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001613 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001614 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001615 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001616 if (!DD->useSplitDwarf()) {
1617 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001618 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001619 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001620 // 2) containing the (relocated) offset of the TLS variable
1621 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001622 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001623 } else {
David Blaikief2443192013-10-21 17:28:37 +00001624 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1625 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001626 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001627 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001628 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikief2694972013-06-28 20:05:11 +00001629 } else
1630 addOpAddress(Block, Sym);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001631 // Do not create specification DIE if context is either compile unit
1632 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001633 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001634 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001635 // Create specification DIE.
David Blaikieeb0338fe2013-11-16 00:28:15 +00001636 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001637 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001638 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001639 // A static member's declaration is already flagged as such.
1640 if (!SDMDecl.Verify())
1641 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001642 } else {
David Blaikief2443192013-10-21 17:28:37 +00001643 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001644 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001645 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001646 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001647 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001648 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1649 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1650 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001651 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1652 : VariableDIE,
1653 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001654 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001655 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001656 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001657 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001658 // emitting AT_const_value multiple times, we only add AT_const_value when
1659 // it is not a static member.
1660 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001661 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001662 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001663 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001664 // GV is a merged global.
1665 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1666 Value *Ptr = CE->getOperand(0);
Rafael Espindola79858aa2013-10-29 17:07:16 +00001667 addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr)));
David Blaikief2443192013-10-21 17:28:37 +00001668 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001669 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001670 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001671 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001672 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1673 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001674 }
1675
Eric Christopherc12c2112011-11-11 01:55:22 +00001676 if (addToAccelTable) {
1677 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1678 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001679
Eric Christopherc12c2112011-11-11 01:55:22 +00001680 // If the linkage name is different than the name, go ahead and output
1681 // that as well into the name table.
1682 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1683 addAccelName(GV.getLinkageName(), AddrDIE);
1684 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001685
1686 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001687 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1688 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001689}
1690
Devang Patel0e821f42011-04-12 23:21:44 +00001691/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopherd42b92f2012-05-22 18:45:24 +00001692void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1693 DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001694 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001695 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001696
Bill Wendling28fe9e72012-12-06 07:38:10 +00001697 // The LowerBound value defines the lower bounds which is typically zero for
1698 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1699 // Count == -1 then the array is unbounded and we do not emit
1700 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1701 // Count == 0, then the array has zero elements in which case we do not emit
1702 // an upper bound.
1703 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001704 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001705 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001706
Bill Wendling3495f9b2012-12-06 07:55:19 +00001707 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001708 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001709
1710 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001711 // FIXME: An unbounded array should reference the expression that defines
1712 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001713 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1714 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001715}
1716
1717/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001718void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
1719 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001720 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001721
Eric Christopher0df08e22013-08-08 07:40:37 +00001722 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001723 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001724
1725 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001726 // FIXME: This type should be passed down from the front end
1727 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001728 DIE *IdxTy = getIndexTyDie();
1729 if (!IdxTy) {
1730 // Construct an anonymous type for index type.
Manman Renb987e512013-10-29 00:53:03 +00001731 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
Eric Christophercad9b532013-01-04 21:51:53 +00001732 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001733 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001734 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1735 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001736 setIndexTyDie(IdxTy);
1737 }
1738
1739 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001740 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001741 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1742 DIDescriptor Element = Elements.getElement(i);
1743 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1744 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1745 }
1746}
1747
Eric Christopheraeb105f2013-11-11 18:52:39 +00001748/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
1749void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
1750 DIArray Elements = CTy.getTypeArray();
1751
1752 // Add enumerators to enumeration type.
1753 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001754 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001755 if (Enum.isEnumerator()) {
1756 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001757 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001758 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001759 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001760 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1761 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001762 }
1763 }
1764 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1765 if (DTy) {
1766 addType(&Buffer, DTy);
1767 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1768 }
Devang Patel0e821f42011-04-12 23:21:44 +00001769}
1770
Devang Patel89543712011-08-15 17:24:54 +00001771/// constructContainingTypeDIEs - Construct DIEs for types that contain
1772/// vtables.
1773void CompileUnit::constructContainingTypeDIEs() {
1774 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001775 CE = ContainingTypeMap.end();
1776 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001777 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001778 DIDescriptor D(CI->second);
1779 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001780 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001781 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001782 if (!NDie)
1783 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001784 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001785 }
1786}
1787
Devang Patel3acc70e2011-08-15 22:04:40 +00001788/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopher34a2c872013-11-15 01:43:19 +00001789DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
1790 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001791
Devang Patel3acc70e2011-08-15 22:04:40 +00001792 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001793 DIE *VariableDie = new DIE(DV.getTag());
1794 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001795 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001796 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001797 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001798 else {
David Blaikie715528b2013-08-19 03:34:03 +00001799 if (!Name.empty())
1800 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001801 addSourceLine(VariableDie, DV.getVariable());
1802 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001803 }
1804
Eric Christopher34a2c872013-11-15 01:43:19 +00001805 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001806 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001807
1808 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001809 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001810 return VariableDie;
1811 }
1812
1813 // Add variable address.
1814
Eric Christopher34a2c872013-11-15 01:43:19 +00001815 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001816 if (Offset != ~0U) {
Eric Christopherd0b82ae2013-11-16 00:18:40 +00001817 addLabel(VariableDie, dwarf::DW_AT_location,
1818 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1819 : dwarf::DW_FORM_data4,
Eric Christopher40b6bf62013-06-24 21:34:55 +00001820 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001821 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001822 return VariableDie;
1823 }
1824
Eric Christophercead0332011-10-03 15:49:20 +00001825 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001826 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001827 assert(DVInsn->getNumOperands() == 3);
1828 if (DVInsn->getOperand(0).isReg()) {
1829 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001830 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001831 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001832 MachineLocation Location(RegOp.getReg(),
1833 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001834 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001835 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001836 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001837 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001838 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001839 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001840 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001841 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001842 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001843 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001844
Eric Christopher34a2c872013-11-15 01:43:19 +00001845 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001846 return VariableDie;
1847 } else {
1848 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001849 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001850 if (FI != ~0) {
1851 unsigned FrameReg = 0;
1852 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001853 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001854 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001855 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001856 }
1857 }
1858
Eric Christopher34a2c872013-11-15 01:43:19 +00001859 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001860 return VariableDie;
1861}
1862
Manman Ren230ec862013-10-23 23:00:44 +00001863/// constructMemberDIE - Construct member DIE from DIDerivedType.
1864void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001865 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001866 StringRef Name = DT.getName();
1867 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001868 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001869
Manman Ren93b30902013-10-08 18:42:58 +00001870 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001871
1872 addSourceLine(MemberDie, DT);
1873
1874 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001875 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001876
Eric Christopherc2697f82013-10-19 01:04:47 +00001877 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001878
1879 // For C++, virtual base classes are not at fixed offset. Use following
1880 // expression to extract appropriate offset from vtable.
1881 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1882
1883 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001884 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1885 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1886 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1887 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1888 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1889 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1890 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001891
David Blaikief2443192013-10-21 17:28:37 +00001892 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001893 } else {
1894 uint64_t Size = DT.getSizeInBits();
1895 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1896 uint64_t OffsetInBytes;
1897
1898 if (Size != FieldSize) {
1899 // Handle bitfield.
1900 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1901 getBaseTypeSize(DD, DT) >> 3);
1902 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1903
1904 uint64_t Offset = DT.getOffsetInBits();
1905 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1906 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1907 uint64_t FieldOffset = (HiMark - FieldSize);
1908 Offset -= FieldOffset;
1909
1910 // Maybe we need to work from the other end.
1911 if (Asm->getDataLayout().isLittleEndian())
1912 Offset = FieldSize - (Offset + Size);
1913 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1914
1915 // Here WD_AT_data_member_location points to the anonymous
1916 // field that includes this bit field.
1917 OffsetInBytes = FieldOffset >> 3;
1918 } else
1919 // This is not a bitfield.
1920 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001921 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001922 }
Devang Patel0e821f42011-04-12 23:21:44 +00001923
1924 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001925 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001926 dwarf::DW_ACCESS_protected);
1927 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001928 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001929 dwarf::DW_ACCESS_private);
1930 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001931 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001932 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001933 dwarf::DW_ACCESS_public);
1934 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001935 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001936 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001937
1938 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001939 if (MDNode *PNode = DT.getObjCProperty())
1940 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001941 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001942 PropertyDie);
1943
David Blaikie37fefc32012-12-13 22:43:07 +00001944 if (DT.isArtificial())
1945 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001946}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001947
Manman Renc6b63922013-10-14 20:33:57 +00001948/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
David Blaikie2ad00162013-11-15 23:09:13 +00001949DIE *CompileUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001950 if (!DT.Verify())
1951 return NULL;
1952
Manman Renc6b63922013-10-14 20:33:57 +00001953 // Construct the context before querying for the existence of the DIE in case
1954 // such construction creates the DIE.
1955 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001956 assert(dwarf::isType(ContextDIE->getTag()) &&
1957 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001958
1959 DIE *StaticMemberDIE = getDIE(DT);
1960 if (StaticMemberDIE)
1961 return StaticMemberDIE;
1962
Manman Renb987e512013-10-29 00:53:03 +00001963 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001964
Manman Ren93b30902013-10-08 18:42:58 +00001965 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001966
1967 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1968 addType(StaticMemberDIE, Ty);
1969 addSourceLine(StaticMemberDIE, DT);
1970 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1971 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1972
1973 // FIXME: We could omit private if the parent is a class_type, and
1974 // public if the parent is something else.
1975 if (DT.isProtected())
1976 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1977 dwarf::DW_ACCESS_protected);
1978 else if (DT.isPrivate())
1979 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1980 dwarf::DW_ACCESS_private);
1981 else
1982 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1983 dwarf::DW_ACCESS_public);
1984
1985 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001986 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001987 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1988 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001989
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001990 return StaticMemberDIE;
1991}
David Blaikie6b288cf2013-10-30 20:42:41 +00001992
1993void CompileUnit::emitHeader(const MCSection *ASection,
1994 const MCSymbol *ASectionSym) {
1995 Asm->OutStreamer.AddComment("DWARF version number");
1996 Asm->EmitInt16(DD->getDwarfVersion());
1997 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1998 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1999 ASectionSym);
2000 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2001 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2002}