blob: c3dcd9c8c19dd370e30c6fefc3b1403e4f264ffe [file] [log] [blame]
Bill Wendling88423ee2009-05-15 00:11:17 +00001//===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
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//
10// Data structures for DWARF info entries.
Eric Christopherff348452013-01-07 22:40:45 +000011//
Bill Wendling88423ee2009-05-15 00:11:17 +000012//===----------------------------------------------------------------------===//
13
14#include "DIE.h"
Manman Ren0e6783f2013-07-02 23:40:10 +000015#include "DwarfDebug.h"
Stephen Hines36b56882014-04-23 16:57:46 -070016#include "DwarfUnit.h"
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +000017#include "llvm/ADT/Twine.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000018#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerbcb83e52010-01-20 07:41:15 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattner858431d2010-01-16 18:50:28 +000022#include "llvm/MC/MCSymbol.h"
David Greene0c8b6e62009-12-24 00:27:55 +000023#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000025#include "llvm/Support/Format.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000026#include "llvm/Support/FormattedStream.h"
Stephen Hines36b56882014-04-23 16:57:46 -070027#include "llvm/Support/LEB128.h"
Eric Christopher3dee5752013-07-26 17:02:41 +000028#include "llvm/Support/MD5.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000029using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// DIEAbbrevData Implementation
33//===----------------------------------------------------------------------===//
34
35/// Profile - Used to gather unique data for the abbreviation folding set.
36///
37void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner82e7eda2013-10-21 19:18:31 +000038 // Explicitly cast to an integer type for which FoldingSetNodeID has
39 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
40 ID.AddInteger(unsigned(Attribute));
41 ID.AddInteger(unsigned(Form));
Bill Wendling88423ee2009-05-15 00:11:17 +000042}
43
44//===----------------------------------------------------------------------===//
45// DIEAbbrev Implementation
46//===----------------------------------------------------------------------===//
47
48/// Profile - Used to gather unique data for the abbreviation folding set.
49///
50void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
Reid Kleckner82e7eda2013-10-21 19:18:31 +000051 ID.AddInteger(unsigned(Tag));
Stephen Hines36b56882014-04-23 16:57:46 -070052 ID.AddInteger(unsigned(Children));
Bill Wendling88423ee2009-05-15 00:11:17 +000053
54 // For each attribute description.
55 for (unsigned i = 0, N = Data.size(); i < N; ++i)
56 Data[i].Profile(ID);
57}
58
59/// Emit - Print the abbreviation using the specified asm printer.
60///
Chris Lattnerd38fee82010-04-05 00:13:49 +000061void DIEAbbrev::Emit(AsmPrinter *AP) const {
Bill Wendling88423ee2009-05-15 00:11:17 +000062 // Emit its Dwarf tag type.
Chris Lattnerd38fee82010-04-05 00:13:49 +000063 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
Bill Wendling88423ee2009-05-15 00:11:17 +000064
65 // Emit whether it has children DIEs.
Stephen Hines36b56882014-04-23 16:57:46 -070066 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
Bill Wendling88423ee2009-05-15 00:11:17 +000067
68 // For each attribute description.
69 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
70 const DIEAbbrevData &AttrData = Data[i];
71
72 // Emit attribute type.
Chris Lattnerd38fee82010-04-05 00:13:49 +000073 AP->EmitULEB128(AttrData.getAttribute(),
Nick Lewycky3bbb6f72011-07-29 03:49:23 +000074 dwarf::AttributeString(AttrData.getAttribute()));
Bill Wendling88423ee2009-05-15 00:11:17 +000075
76 // Emit form type.
Chris Lattnerd38fee82010-04-05 00:13:49 +000077 AP->EmitULEB128(AttrData.getForm(),
78 dwarf::FormEncodingString(AttrData.getForm()));
Bill Wendling88423ee2009-05-15 00:11:17 +000079 }
80
81 // Mark end of abbreviation.
Chris Lattnerd38fee82010-04-05 00:13:49 +000082 AP->EmitULEB128(0, "EOM(1)");
83 AP->EmitULEB128(0, "EOM(2)");
Bill Wendling88423ee2009-05-15 00:11:17 +000084}
85
86#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +000087void DIEAbbrev::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +000088 O << "Abbreviation @"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000089 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +000090 << " "
91 << dwarf::TagString(Tag)
92 << " "
Stephen Hines36b56882014-04-23 16:57:46 -070093 << dwarf::ChildrenString(Children)
Chris Lattnerb01acfa2009-08-23 01:01:17 +000094 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +000095
96 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
97 O << " "
98 << dwarf::AttributeString(Data[i].getAttribute())
99 << " "
100 << dwarf::FormEncodingString(Data[i].getForm())
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000101 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000102 }
103}
David Greene0c8b6e62009-12-24 00:27:55 +0000104void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling88423ee2009-05-15 00:11:17 +0000105#endif
106
Stephen Hines36b56882014-04-23 16:57:46 -0700107/// Climb up the parent chain to get the unit DIE to which this DIE
Manman Ren3eabc2a2013-10-29 22:57:10 +0000108/// belongs.
Stephen Hines36b56882014-04-23 16:57:46 -0700109const DIE *DIE::getUnit() const {
110 const DIE *Cu = getUnitOrNull();
Manman Renb8b70e12013-10-31 17:54:35 +0000111 assert(Cu && "We should not have orphaned DIEs.");
112 return Cu;
113}
114
Stephen Hines36b56882014-04-23 16:57:46 -0700115/// Climb up the parent chain to get the unit DIE this DIE belongs
Manman Renb8b70e12013-10-31 17:54:35 +0000116/// to. Return NULL if DIE is not added to an owner yet.
Stephen Hines36b56882014-04-23 16:57:46 -0700117const DIE *DIE::getUnitOrNull() const {
Manman Ren3eabc2a2013-10-29 22:57:10 +0000118 const DIE *p = this;
119 while (p) {
Stephen Hines36b56882014-04-23 16:57:46 -0700120 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
121 p->getTag() == dwarf::DW_TAG_type_unit)
Manman Ren3eabc2a2013-10-29 22:57:10 +0000122 return p;
123 p = p->getParent();
124 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700125 return nullptr;
Manman Ren3eabc2a2013-10-29 22:57:10 +0000126}
127
Stephen Hines36b56882014-04-23 16:57:46 -0700128DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
Eric Christopherb7669132013-08-07 01:18:33 +0000129 const SmallVectorImpl<DIEValue *> &Values = getValues();
130 const DIEAbbrev &Abbrevs = getAbbrev();
131
132 // Iterate through all the attributes until we find the one we're
133 // looking for, if we can't find it return NULL.
134 for (size_t i = 0; i < Values.size(); ++i)
135 if (Abbrevs.getData()[i].getAttribute() == Attribute)
136 return Values[i];
Stephen Hinesdce4a402014-05-29 02:49:00 -0700137 return nullptr;
Eric Christopherb7669132013-08-07 01:18:33 +0000138}
139
Bill Wendling88423ee2009-05-15 00:11:17 +0000140#ifndef NDEBUG
Eric Christopher30cb8362013-05-06 17:50:50 +0000141void DIE::print(raw_ostream &O, unsigned IndentCount) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000142 const std::string Indent(IndentCount, ' ');
143 bool isBlock = Abbrev.getTag() == 0;
144
145 if (!isBlock) {
146 O << Indent
147 << "Die: "
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000148 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +0000149 << ", Offset: " << Offset
Chris Lattner0d9d70f2010-03-09 23:38:23 +0000150 << ", Size: " << Size << "\n";
Bill Wendling88423ee2009-05-15 00:11:17 +0000151
152 O << Indent
153 << dwarf::TagString(Abbrev.getTag())
154 << " "
Stephen Hines36b56882014-04-23 16:57:46 -0700155 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
Bill Wendling88423ee2009-05-15 00:11:17 +0000156 } else {
Chris Lattner0d9d70f2010-03-09 23:38:23 +0000157 O << "Size: " << Size << "\n";
Bill Wendling88423ee2009-05-15 00:11:17 +0000158 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000159
Eric Christopherf7cef702013-03-29 23:34:06 +0000160 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
Bill Wendling88423ee2009-05-15 00:11:17 +0000161
162 IndentCount += 2;
163 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
164 O << Indent;
165
166 if (!isBlock)
167 O << dwarf::AttributeString(Data[i].getAttribute());
168 else
169 O << "Blk[" << i << "]";
170
171 O << " "
172 << dwarf::FormEncodingString(Data[i].getForm())
173 << " ";
174 Values[i]->print(O);
175 O << "\n";
176 }
177 IndentCount -= 2;
178
179 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Eric Christopher30cb8362013-05-06 17:50:50 +0000180 Children[j]->print(O, IndentCount+4);
Bill Wendling88423ee2009-05-15 00:11:17 +0000181 }
182
183 if (!isBlock) O << "\n";
Bill Wendling88423ee2009-05-15 00:11:17 +0000184}
185
186void DIE::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000187 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000188}
189#endif
190
David Blaikie2d24e2a2011-12-20 02:50:00 +0000191void DIEValue::anchor() { }
Bill Wendling88423ee2009-05-15 00:11:17 +0000192
193#ifndef NDEBUG
Eric Christopher813419e2013-05-31 22:50:40 +0000194void DIEValue::dump() const {
David Greene0c8b6e62009-12-24 00:27:55 +0000195 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000196}
197#endif
198
199//===----------------------------------------------------------------------===//
200// DIEInteger Implementation
201//===----------------------------------------------------------------------===//
202
203/// EmitValue - Emit integer of appropriate size.
204///
David Blaikie770530b2013-10-21 17:28:37 +0000205void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000206 unsigned Size = ~0U;
Bill Wendling88423ee2009-05-15 00:11:17 +0000207 switch (Form) {
Eric Christopher873cf0a2012-08-24 01:14:27 +0000208 case dwarf::DW_FORM_flag_present:
209 // Emit something to keep the lines and comments in sync.
210 // FIXME: Is there a better way to do this?
Stephen Hines36b56882014-04-23 16:57:46 -0700211 Asm->OutStreamer.AddBlankLine();
Eric Christopher873cf0a2012-08-24 01:14:27 +0000212 return;
Bill Wendling88423ee2009-05-15 00:11:17 +0000213 case dwarf::DW_FORM_flag: // Fall thru
214 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000215 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000216 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000217 case dwarf::DW_FORM_data2: Size = 2; break;
Eric Christopherd96c72a2013-01-17 02:59:59 +0000218 case dwarf::DW_FORM_sec_offset: // Fall thru
Bill Wendling88423ee2009-05-15 00:11:17 +0000219 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000220 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000221 case dwarf::DW_FORM_ref8: // Fall thru
Stephen Hines36b56882014-04-23 16:57:46 -0700222 case dwarf::DW_FORM_ref_sig8: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000223 case dwarf::DW_FORM_data8: Size = 8; break;
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000224 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000225 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000226 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
227 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
Eric Christopher4984e012012-09-10 23:34:03 +0000228 case dwarf::DW_FORM_addr:
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000229 Size = Asm->getDataLayout().getPointerSize(); break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000230 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling88423ee2009-05-15 00:11:17 +0000231 }
Eric Christopherca1dd052013-01-09 01:35:34 +0000232 Asm->OutStreamer.EmitIntValue(Integer, Size);
Bill Wendling88423ee2009-05-15 00:11:17 +0000233}
234
235/// SizeOf - Determine size of integer value in bytes.
236///
David Blaikie770530b2013-10-21 17:28:37 +0000237unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000238 switch (Form) {
Eric Christopher17dd9a92012-08-29 17:59:32 +0000239 case dwarf::DW_FORM_flag_present: return 0;
Bill Wendling88423ee2009-05-15 00:11:17 +0000240 case dwarf::DW_FORM_flag: // Fall thru
241 case dwarf::DW_FORM_ref1: // Fall thru
242 case dwarf::DW_FORM_data1: return sizeof(int8_t);
243 case dwarf::DW_FORM_ref2: // Fall thru
244 case dwarf::DW_FORM_data2: return sizeof(int16_t);
Eric Christopherd96c72a2013-01-17 02:59:59 +0000245 case dwarf::DW_FORM_sec_offset: // Fall thru
Bill Wendling88423ee2009-05-15 00:11:17 +0000246 case dwarf::DW_FORM_ref4: // Fall thru
247 case dwarf::DW_FORM_data4: return sizeof(int32_t);
248 case dwarf::DW_FORM_ref8: // Fall thru
Stephen Hines36b56882014-04-23 16:57:46 -0700249 case dwarf::DW_FORM_ref_sig8: // Fall thru
Bill Wendling88423ee2009-05-15 00:11:17 +0000250 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Stephen Hines36b56882014-04-23 16:57:46 -0700251 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
252 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
253 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
254 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000255 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
David Blaikie4d6ccb52012-01-20 21:51:11 +0000256 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling88423ee2009-05-15 00:11:17 +0000257 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000258}
259
Bill Wendling88423ee2009-05-15 00:11:17 +0000260#ifndef NDEBUG
Eric Christopher813419e2013-05-31 22:50:40 +0000261void DIEInteger::print(raw_ostream &O) const {
Benjamin Kramer41a96492011-11-05 08:57:40 +0000262 O << "Int: " << (int64_t)Integer << " 0x";
263 O.write_hex(Integer);
Bill Wendling88423ee2009-05-15 00:11:17 +0000264}
265#endif
266
267//===----------------------------------------------------------------------===//
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000268// DIEExpr Implementation
269//===----------------------------------------------------------------------===//
270
271/// EmitValue - Emit expression value.
272///
David Blaikie770530b2013-10-21 17:28:37 +0000273void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000274 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
275}
276
277/// SizeOf - Determine size of expression value in bytes.
278///
David Blaikie770530b2013-10-21 17:28:37 +0000279unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000280 if (Form == dwarf::DW_FORM_data4) return 4;
281 if (Form == dwarf::DW_FORM_sec_offset) return 4;
282 if (Form == dwarf::DW_FORM_strp) return 4;
283 return AP->getDataLayout().getPointerSize();
284}
285
286#ifndef NDEBUG
287void DIEExpr::print(raw_ostream &O) const {
288 O << "Expr: ";
289 Expr->print(O);
290}
291#endif
292
293//===----------------------------------------------------------------------===//
Chris Lattner4faf59a2010-03-08 22:31:46 +0000294// DIELabel Implementation
Bill Wendling88423ee2009-05-15 00:11:17 +0000295//===----------------------------------------------------------------------===//
296
297/// EmitValue - Emit label value.
298///
David Blaikie770530b2013-10-21 17:28:37 +0000299void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
300 AP->EmitLabelReference(Label, SizeOf(AP, Form),
301 Form == dwarf::DW_FORM_strp ||
302 Form == dwarf::DW_FORM_sec_offset ||
303 Form == dwarf::DW_FORM_ref_addr);
Bill Wendling88423ee2009-05-15 00:11:17 +0000304}
305
306/// SizeOf - Determine size of label value in bytes.
307///
David Blaikie770530b2013-10-21 17:28:37 +0000308unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000309 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher446b88f2013-01-17 03:00:04 +0000310 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewycky390c40d2011-10-27 06:44:11 +0000311 if (Form == dwarf::DW_FORM_strp) return 4;
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000312 return AP->getDataLayout().getPointerSize();
Bill Wendling88423ee2009-05-15 00:11:17 +0000313}
314
Bill Wendling88423ee2009-05-15 00:11:17 +0000315#ifndef NDEBUG
Eric Christopher813419e2013-05-31 22:50:40 +0000316void DIELabel::print(raw_ostream &O) const {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000317 O << "Lbl: " << Label->getName();
Bill Wendling88423ee2009-05-15 00:11:17 +0000318}
319#endif
320
321//===----------------------------------------------------------------------===//
Bill Wendling88423ee2009-05-15 00:11:17 +0000322// DIEDelta Implementation
323//===----------------------------------------------------------------------===//
324
325/// EmitValue - Emit delta value.
326///
David Blaikie770530b2013-10-21 17:28:37 +0000327void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattnera37d5382010-04-05 00:18:22 +0000328 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling88423ee2009-05-15 00:11:17 +0000329}
330
331/// SizeOf - Determine size of delta value in bytes.
332///
David Blaikie770530b2013-10-21 17:28:37 +0000333unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000334 if (Form == dwarf::DW_FORM_data4) return 4;
Stephen Hines36b56882014-04-23 16:57:46 -0700335 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewycky390c40d2011-10-27 06:44:11 +0000336 if (Form == dwarf::DW_FORM_strp) return 4;
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000337 return AP->getDataLayout().getPointerSize();
Bill Wendling88423ee2009-05-15 00:11:17 +0000338}
339
Bill Wendling88423ee2009-05-15 00:11:17 +0000340#ifndef NDEBUG
Eric Christopher813419e2013-05-31 22:50:40 +0000341void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerb98b1bf2010-03-08 22:23:36 +0000342 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling88423ee2009-05-15 00:11:17 +0000343}
344#endif
345
346//===----------------------------------------------------------------------===//
Eric Christopher3dee5752013-07-26 17:02:41 +0000347// DIEString Implementation
348//===----------------------------------------------------------------------===//
349
350/// EmitValue - Emit string value.
351///
David Blaikie770530b2013-10-21 17:28:37 +0000352void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher3dee5752013-07-26 17:02:41 +0000353 Access->EmitValue(AP, Form);
354}
355
356/// SizeOf - Determine size of delta value in bytes.
357///
David Blaikie770530b2013-10-21 17:28:37 +0000358unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher3dee5752013-07-26 17:02:41 +0000359 return Access->SizeOf(AP, Form);
360}
361
362#ifndef NDEBUG
363void DIEString::print(raw_ostream &O) const {
364 O << "String: " << Str << "\tSymbol: ";
365 Access->print(O);
366}
367#endif
368
369//===----------------------------------------------------------------------===//
Bill Wendling88423ee2009-05-15 00:11:17 +0000370// DIEEntry Implementation
371//===----------------------------------------------------------------------===//
372
373/// EmitValue - Emit debug information entry offset.
374///
David Blaikie770530b2013-10-21 17:28:37 +0000375void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Stephen Hines36b56882014-04-23 16:57:46 -0700376
377 if (Form == dwarf::DW_FORM_ref_addr) {
378 const DwarfDebug *DD = AP->getDwarfDebug();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700379 unsigned Addr = Entry.getOffset();
Stephen Hines36b56882014-04-23 16:57:46 -0700380 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
381 // For DW_FORM_ref_addr, output the offset from beginning of debug info
382 // section. Entry->getOffset() returns the offset from start of the
383 // compile unit.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700384 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
Stephen Hines36b56882014-04-23 16:57:46 -0700385 assert(CU && "CUDie should belong to a CU.");
386 Addr += CU->getDebugInfoOffset();
387 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
388 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
389 DIEEntry::getRefAddrSize(AP));
390 else
391 AP->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
392 CU->getSectionSym(),
393 DIEEntry::getRefAddrSize(AP));
394 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700395 AP->EmitInt32(Entry.getOffset());
Bill Wendling88423ee2009-05-15 00:11:17 +0000396}
397
Manman Ren0e6783f2013-07-02 23:40:10 +0000398unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
399 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
400 // specified to be four bytes in the DWARF 32-bit format and eight bytes
401 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
402 // references have the same size as an address on the target system.
Stephen Hines36b56882014-04-23 16:57:46 -0700403 const DwarfDebug *DD = AP->getDwarfDebug();
404 assert(DD && "Expected Dwarf Debug info to be available");
405 if (DD->getDwarfVersion() == 2)
Manman Ren0e6783f2013-07-02 23:40:10 +0000406 return AP->getDataLayout().getPointerSize();
407 return sizeof(int32_t);
408}
409
Bill Wendling88423ee2009-05-15 00:11:17 +0000410#ifndef NDEBUG
Eric Christopher813419e2013-05-31 22:50:40 +0000411void DIEEntry::print(raw_ostream &O) const {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700412 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling88423ee2009-05-15 00:11:17 +0000413}
414#endif
415
416//===----------------------------------------------------------------------===//
Stephen Hines36b56882014-04-23 16:57:46 -0700417// DIETypeSignature Implementation
418//===----------------------------------------------------------------------===//
419void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
420 assert(Form == dwarf::DW_FORM_ref_sig8);
421 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
422}
423
424#ifndef NDEBUG
425void DIETypeSignature::print(raw_ostream &O) const {
426 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
427}
428
429void DIETypeSignature::dump() const { print(dbgs()); }
430#endif
431
432//===----------------------------------------------------------------------===//
433// DIELoc Implementation
434//===----------------------------------------------------------------------===//
435
436/// ComputeSize - calculate the size of the location expression.
437///
438unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
439 if (!Size) {
440 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
441 for (unsigned i = 0, N = Values.size(); i < N; ++i)
442 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
443 }
444
445 return Size;
446}
447
448/// EmitValue - Emit location data.
449///
450void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
451 switch (Form) {
452 default: llvm_unreachable("Improper form for block");
453 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
454 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
455 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
456 case dwarf::DW_FORM_block:
457 case dwarf::DW_FORM_exprloc:
458 Asm->EmitULEB128(Size); break;
459 }
460
461 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
462 for (unsigned i = 0, N = Values.size(); i < N; ++i)
463 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
464}
465
466/// SizeOf - Determine size of location data in bytes.
467///
468unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
469 switch (Form) {
470 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
471 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
472 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
473 case dwarf::DW_FORM_block:
474 case dwarf::DW_FORM_exprloc:
475 return Size + getULEB128Size(Size);
476 default: llvm_unreachable("Improper form for block");
477 }
478}
479
480#ifndef NDEBUG
481void DIELoc::print(raw_ostream &O) const {
482 O << "ExprLoc: ";
483 DIE::print(O, 5);
484}
485#endif
486
487//===----------------------------------------------------------------------===//
Bill Wendling88423ee2009-05-15 00:11:17 +0000488// DIEBlock Implementation
489//===----------------------------------------------------------------------===//
490
491/// ComputeSize - calculate the size of the block.
492///
Stephen Hines36b56882014-04-23 16:57:46 -0700493unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000494 if (!Size) {
Eric Christopherf7cef702013-03-29 23:34:06 +0000495 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling88423ee2009-05-15 00:11:17 +0000496 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Chris Lattnera37d5382010-04-05 00:18:22 +0000497 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
Bill Wendling88423ee2009-05-15 00:11:17 +0000498 }
499
500 return Size;
501}
502
503/// EmitValue - Emit block data.
504///
David Blaikie770530b2013-10-21 17:28:37 +0000505void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000506 switch (Form) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000507 default: llvm_unreachable("Improper form for block");
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000508 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
509 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
510 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
511 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000512 }
513
Eric Christopherf7cef702013-03-29 23:34:06 +0000514 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Chris Lattner0d9d70f2010-03-09 23:38:23 +0000515 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Chris Lattnerd38fee82010-04-05 00:13:49 +0000516 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
Bill Wendling88423ee2009-05-15 00:11:17 +0000517}
518
519/// SizeOf - Determine size of block data in bytes.
520///
David Blaikie770530b2013-10-21 17:28:37 +0000521unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000522 switch (Form) {
523 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
524 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
525 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Stephen Hines36b56882014-04-23 16:57:46 -0700526 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
David Blaikie4d6ccb52012-01-20 21:51:11 +0000527 default: llvm_unreachable("Improper form for block");
Bill Wendling88423ee2009-05-15 00:11:17 +0000528 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000529}
530
Bill Wendling88423ee2009-05-15 00:11:17 +0000531#ifndef NDEBUG
Eric Christopher813419e2013-05-31 22:50:40 +0000532void DIEBlock::print(raw_ostream &O) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000533 O << "Blk: ";
534 DIE::print(O, 5);
535}
536#endif
Stephen Hines36b56882014-04-23 16:57:46 -0700537
538//===----------------------------------------------------------------------===//
539// DIELocList Implementation
540//===----------------------------------------------------------------------===//
541
542unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
543 if (Form == dwarf::DW_FORM_data4)
544 return 4;
545 if (Form == dwarf::DW_FORM_sec_offset)
546 return 4;
547 return AP->getDataLayout().getPointerSize();
548}
549
550/// EmitValue - Emit label value.
551///
552void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
553 DwarfDebug *DD = AP->getDwarfDebug();
554 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
555
556 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
557 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
558 else
559 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
560}
561
562#ifndef NDEBUG
563void DIELocList::print(raw_ostream &O) const {
564 O << "LocList: " << Index;
565
566}
567#endif