blob: 3debff803ba530461a98ab4fcd36c0daf37f980b [file] [log] [blame]
Bill Wendling47054f32009-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 Christopherb800ff72013-01-07 22:40:45 +000011//
Bill Wendling47054f32009-05-15 00:11:17 +000012//===----------------------------------------------------------------------===//
13
Frederic Risse541e0b2015-01-05 21:29:41 +000014#include "llvm/CodeGen/DIE.h"
David Blaikie37c52312014-10-04 15:49:50 +000015#include "DwarfCompileUnit.h"
Manman Renac8062b2013-07-02 23:40:10 +000016#include "DwarfDebug.h"
David Blaikie47f615e2013-12-17 23:32:35 +000017#include "DwarfUnit.h"
Benjamin Kramer4d128a22010-01-17 07:46:39 +000018#include "llvm/ADT/Twine.h"
Bill Wendling47054f32009-05-15 00:11:17 +000019#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Rafael Espindola74dd8542014-10-21 00:25:49 +000022#include "llvm/MC/MCContext.h"
Chris Lattner71601e82010-01-20 07:41:15 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner06d45f62010-01-16 18:50:28 +000024#include "llvm/MC/MCSymbol.h"
David Greene8bc072c2009-12-24 00:27:55 +000025#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner74725712009-08-23 01:01:17 +000027#include "llvm/Support/Format.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000028#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000029#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000030#include "llvm/Support/MD5.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000031#include "llvm/Support/raw_ostream.h"
Bill Wendling47054f32009-05-15 00:11:17 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// DIEAbbrevData Implementation
36//===----------------------------------------------------------------------===//
37
38/// Profile - Used to gather unique data for the abbreviation folding set.
39///
40void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000041 // Explicitly cast to an integer type for which FoldingSetNodeID has
42 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
43 ID.AddInteger(unsigned(Attribute));
44 ID.AddInteger(unsigned(Form));
Bill Wendling47054f32009-05-15 00:11:17 +000045}
46
47//===----------------------------------------------------------------------===//
48// DIEAbbrev Implementation
49//===----------------------------------------------------------------------===//
50
51/// Profile - Used to gather unique data for the abbreviation folding set.
52///
53void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000054 ID.AddInteger(unsigned(Tag));
Eric Christophere8f10722014-03-05 01:44:58 +000055 ID.AddInteger(unsigned(Children));
Bill Wendling47054f32009-05-15 00:11:17 +000056
57 // For each attribute description.
58 for (unsigned i = 0, N = Data.size(); i < N; ++i)
59 Data[i].Profile(ID);
60}
61
62/// Emit - Print the abbreviation using the specified asm printer.
63///
Frederic Risscd044342015-03-04 02:30:08 +000064void DIEAbbrev::Emit(const AsmPrinter *AP) const {
Bill Wendling47054f32009-05-15 00:11:17 +000065 // Emit its Dwarf tag type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000066 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
Bill Wendling47054f32009-05-15 00:11:17 +000067
68 // Emit whether it has children DIEs.
Eric Christophere8f10722014-03-05 01:44:58 +000069 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
Bill Wendling47054f32009-05-15 00:11:17 +000070
71 // For each attribute description.
72 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
73 const DIEAbbrevData &AttrData = Data[i];
74
75 // Emit attribute type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000076 AP->EmitULEB128(AttrData.getAttribute(),
Nick Lewycky019d2552011-07-29 03:49:23 +000077 dwarf::AttributeString(AttrData.getAttribute()));
Bill Wendling47054f32009-05-15 00:11:17 +000078
79 // Emit form type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000080 AP->EmitULEB128(AttrData.getForm(),
81 dwarf::FormEncodingString(AttrData.getForm()));
Bill Wendling47054f32009-05-15 00:11:17 +000082 }
83
84 // Mark end of abbreviation.
Chris Lattner3a383cb2010-04-05 00:13:49 +000085 AP->EmitULEB128(0, "EOM(1)");
86 AP->EmitULEB128(0, "EOM(2)");
Bill Wendling47054f32009-05-15 00:11:17 +000087}
88
89#ifndef NDEBUG
Chris Lattner74725712009-08-23 01:01:17 +000090void DIEAbbrev::print(raw_ostream &O) {
Bill Wendling47054f32009-05-15 00:11:17 +000091 O << "Abbreviation @"
Chris Lattner74725712009-08-23 01:01:17 +000092 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling47054f32009-05-15 00:11:17 +000093 << " "
94 << dwarf::TagString(Tag)
95 << " "
Eric Christophere8f10722014-03-05 01:44:58 +000096 << dwarf::ChildrenString(Children)
Chris Lattner74725712009-08-23 01:01:17 +000097 << '\n';
Bill Wendling47054f32009-05-15 00:11:17 +000098
99 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
100 O << " "
101 << dwarf::AttributeString(Data[i].getAttribute())
102 << " "
103 << dwarf::FormEncodingString(Data[i].getForm())
Chris Lattner74725712009-08-23 01:01:17 +0000104 << '\n';
Bill Wendling47054f32009-05-15 00:11:17 +0000105 }
106}
David Greene8bc072c2009-12-24 00:27:55 +0000107void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling47054f32009-05-15 00:11:17 +0000108#endif
109
Eric Christopherd89221e2013-11-21 01:01:30 +0000110/// Climb up the parent chain to get the unit DIE to which this DIE
Manman Rence20d462013-10-29 22:57:10 +0000111/// belongs.
David Blaikie409dd9c2013-11-19 23:08:21 +0000112const DIE *DIE::getUnit() const {
113 const DIE *Cu = getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000114 assert(Cu && "We should not have orphaned DIEs.");
115 return Cu;
116}
117
Eric Christopherd89221e2013-11-21 01:01:30 +0000118/// Climb up the parent chain to get the unit DIE this DIE belongs
Manman Ren4dbdc902013-10-31 17:54:35 +0000119/// to. Return NULL if DIE is not added to an owner yet.
David Blaikie409dd9c2013-11-19 23:08:21 +0000120const DIE *DIE::getUnitOrNull() const {
Manman Rence20d462013-10-29 22:57:10 +0000121 const DIE *p = this;
122 while (p) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000123 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
124 p->getTag() == dwarf::DW_TAG_type_unit)
Manman Rence20d462013-10-29 22:57:10 +0000125 return p;
126 p = p->getParent();
127 }
Craig Topper353eda42014-04-24 06:44:33 +0000128 return nullptr;
Manman Rence20d462013-10-29 22:57:10 +0000129}
130
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000131DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
132 const SmallVectorImpl<DIEValue *> &Values = getValues();
Eric Christopher8552e222013-08-07 01:18:33 +0000133 const DIEAbbrev &Abbrevs = getAbbrev();
134
135 // Iterate through all the attributes until we find the one we're
136 // looking for, if we can't find it return NULL.
137 for (size_t i = 0; i < Values.size(); ++i)
138 if (Abbrevs.getData()[i].getAttribute() == Attribute)
139 return Values[i];
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000140 return nullptr;
Eric Christopher8552e222013-08-07 01:18:33 +0000141}
142
Bill Wendling47054f32009-05-15 00:11:17 +0000143#ifndef NDEBUG
Eric Christopher6c6de842013-05-06 17:50:50 +0000144void DIE::print(raw_ostream &O, unsigned IndentCount) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000145 const std::string Indent(IndentCount, ' ');
146 bool isBlock = Abbrev.getTag() == 0;
147
148 if (!isBlock) {
149 O << Indent
150 << "Die: "
Chris Lattner74725712009-08-23 01:01:17 +0000151 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling47054f32009-05-15 00:11:17 +0000152 << ", Offset: " << Offset
Chris Lattner3d72a672010-03-09 23:38:23 +0000153 << ", Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000154
155 O << Indent
156 << dwarf::TagString(Abbrev.getTag())
157 << " "
Eric Christophere8f10722014-03-05 01:44:58 +0000158 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000159 } else {
Chris Lattner3d72a672010-03-09 23:38:23 +0000160 O << "Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000161 }
Bill Wendling47054f32009-05-15 00:11:17 +0000162
Eric Christopher4887c8f2013-03-29 23:34:06 +0000163 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
Bill Wendling47054f32009-05-15 00:11:17 +0000164
165 IndentCount += 2;
166 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
167 O << Indent;
168
169 if (!isBlock)
170 O << dwarf::AttributeString(Data[i].getAttribute());
171 else
172 O << "Blk[" << i << "]";
173
174 O << " "
175 << dwarf::FormEncodingString(Data[i].getForm())
176 << " ";
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000177 Values[i]->print(O);
Bill Wendling47054f32009-05-15 00:11:17 +0000178 O << "\n";
179 }
180 IndentCount -= 2;
181
182 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Eric Christopher6c6de842013-05-06 17:50:50 +0000183 Children[j]->print(O, IndentCount+4);
Bill Wendling47054f32009-05-15 00:11:17 +0000184 }
185
186 if (!isBlock) O << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000187}
188
189void DIE::dump() {
David Greene8bc072c2009-12-24 00:27:55 +0000190 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000191}
192#endif
193
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000194void DIEValue::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
195 switch (Ty) {
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000196#define HANDLE_DIEVALUE(T) \
197 case is##T: \
198 cast<DIE##T>(this)->EmitValueImpl(AP, Form); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000199 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000200#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000201 }
202}
203
204unsigned DIEValue::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
205 switch (Ty) {
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000206#define HANDLE_DIEVALUE(T) \
207 case is##T: \
208 return cast<DIE##T>(this)->SizeOfImpl(AP, Form);
209#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000210 }
Aaron Ballmanc681c3d2015-05-23 14:46:49 +0000211 llvm_unreachable("Unknown DIE kind");
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000212}
Bill Wendling47054f32009-05-15 00:11:17 +0000213
214#ifndef NDEBUG
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000215void DIEValue::print(raw_ostream &O) const {
216 switch (Ty) {
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000217#define HANDLE_DIEVALUE(T) \
218 case is##T: \
219 cast<DIE##T>(this)->printImpl(O); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000220 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000221#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000222 }
223}
224
Eric Christopher65ac02a2013-05-31 22:50:40 +0000225void DIEValue::dump() const {
David Greene8bc072c2009-12-24 00:27:55 +0000226 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000227}
228#endif
229
230//===----------------------------------------------------------------------===//
231// DIEInteger Implementation
232//===----------------------------------------------------------------------===//
233
234/// EmitValue - Emit integer of appropriate size.
235///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000236void DIEInteger::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
Chris Lattner71601e82010-01-20 07:41:15 +0000237 unsigned Size = ~0U;
Bill Wendling47054f32009-05-15 00:11:17 +0000238 switch (Form) {
Eric Christopherbb69a272012-08-24 01:14:27 +0000239 case dwarf::DW_FORM_flag_present:
240 // Emit something to keep the lines and comments in sync.
241 // FIXME: Is there a better way to do this?
Lang Hames9ff69c82015-04-24 19:11:51 +0000242 Asm->OutStreamer->AddBlankLine();
Eric Christopherbb69a272012-08-24 01:14:27 +0000243 return;
Bill Wendling47054f32009-05-15 00:11:17 +0000244 case dwarf::DW_FORM_flag: // Fall thru
245 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000246 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling47054f32009-05-15 00:11:17 +0000247 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000248 case dwarf::DW_FORM_data2: Size = 2; break;
Eric Christopher18266172013-01-17 02:59:59 +0000249 case dwarf::DW_FORM_sec_offset: // Fall thru
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000250 case dwarf::DW_FORM_strp: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000251 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000252 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling47054f32009-05-15 00:11:17 +0000253 case dwarf::DW_FORM_ref8: // Fall thru
David Blaikie409dd9c2013-11-19 23:08:21 +0000254 case dwarf::DW_FORM_ref_sig8: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000255 case dwarf::DW_FORM_data8: Size = 8; break;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000256 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
Eric Christopher962c9082013-01-15 23:56:56 +0000257 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
Chris Lattner9efd1182010-04-04 19:09:29 +0000258 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
259 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
Eric Christophere8a7b1b2012-09-10 23:34:03 +0000260 case dwarf::DW_FORM_addr:
Chandler Carruth5da3f052012-11-01 09:14:31 +0000261 Size = Asm->getDataLayout().getPointerSize(); break;
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000262 case dwarf::DW_FORM_ref_addr:
263 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
264 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000265 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000266 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000267 Asm->OutStreamer->EmitIntValue(Integer, Size);
Bill Wendling47054f32009-05-15 00:11:17 +0000268}
269
270/// SizeOf - Determine size of integer value in bytes.
271///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000272unsigned DIEInteger::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000273 switch (Form) {
Eric Christopher2a4e6162012-08-29 17:59:32 +0000274 case dwarf::DW_FORM_flag_present: return 0;
Bill Wendling47054f32009-05-15 00:11:17 +0000275 case dwarf::DW_FORM_flag: // Fall thru
276 case dwarf::DW_FORM_ref1: // Fall thru
277 case dwarf::DW_FORM_data1: return sizeof(int8_t);
278 case dwarf::DW_FORM_ref2: // Fall thru
279 case dwarf::DW_FORM_data2: return sizeof(int16_t);
Eric Christopher18266172013-01-17 02:59:59 +0000280 case dwarf::DW_FORM_sec_offset: // Fall thru
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000281 case dwarf::DW_FORM_strp: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000282 case dwarf::DW_FORM_ref4: // Fall thru
283 case dwarf::DW_FORM_data4: return sizeof(int32_t);
284 case dwarf::DW_FORM_ref8: // Fall thru
David Blaikie409dd9c2013-11-19 23:08:21 +0000285 case dwarf::DW_FORM_ref_sig8: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000286 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000287 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
288 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
289 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
290 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
Chandler Carruth5da3f052012-11-01 09:14:31 +0000291 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000292 case dwarf::DW_FORM_ref_addr:
Lang Hames9ff69c82015-04-24 19:11:51 +0000293 if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000294 return AP->getDataLayout().getPointerSize();
295 return sizeof(int32_t);
David Blaikie46a9f012012-01-20 21:51:11 +0000296 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000297 }
Bill Wendling47054f32009-05-15 00:11:17 +0000298}
299
Bill Wendling47054f32009-05-15 00:11:17 +0000300#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000301void DIEInteger::printImpl(raw_ostream &O) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000302 O << "Int: " << (int64_t)Integer << " 0x";
303 O.write_hex(Integer);
Bill Wendling47054f32009-05-15 00:11:17 +0000304}
305#endif
306
307//===----------------------------------------------------------------------===//
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000308// DIEExpr Implementation
309//===----------------------------------------------------------------------===//
310
311/// EmitValue - Emit expression value.
312///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000313void DIEExpr::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000314 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000315}
316
317/// SizeOf - Determine size of expression value in bytes.
318///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000319unsigned DIEExpr::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000320 if (Form == dwarf::DW_FORM_data4) return 4;
321 if (Form == dwarf::DW_FORM_sec_offset) return 4;
322 if (Form == dwarf::DW_FORM_strp) return 4;
323 return AP->getDataLayout().getPointerSize();
324}
325
326#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000327void DIEExpr::printImpl(raw_ostream &O) const { O << "Expr: " << *Expr; }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000328#endif
329
330//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000331// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000332//===----------------------------------------------------------------------===//
333
334/// EmitValue - Emit label value.
335///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000336void DIELabel::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikief2443192013-10-21 17:28:37 +0000337 AP->EmitLabelReference(Label, SizeOf(AP, Form),
338 Form == dwarf::DW_FORM_strp ||
339 Form == dwarf::DW_FORM_sec_offset ||
340 Form == dwarf::DW_FORM_ref_addr);
Bill Wendling47054f32009-05-15 00:11:17 +0000341}
342
343/// SizeOf - Determine size of label value in bytes.
344///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000345unsigned DIELabel::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000346 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher4c7765f2013-01-17 03:00:04 +0000347 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000348 if (Form == dwarf::DW_FORM_strp) return 4;
Chandler Carruth5da3f052012-11-01 09:14:31 +0000349 return AP->getDataLayout().getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000350}
351
Bill Wendling47054f32009-05-15 00:11:17 +0000352#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000353void DIELabel::printImpl(raw_ostream &O) const {
354 O << "Lbl: " << Label->getName();
355}
Bill Wendling47054f32009-05-15 00:11:17 +0000356#endif
357
358//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000359// DIEDelta Implementation
360//===----------------------------------------------------------------------===//
361
362/// EmitValue - Emit delta value.
363///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000364void DIEDelta::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000365 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000366}
367
368/// SizeOf - Determine size of delta value in bytes.
369///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000370unsigned DIEDelta::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000371 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher33ff6972013-11-21 23:46:41 +0000372 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000373 if (Form == dwarf::DW_FORM_strp) return 4;
Chandler Carruth5da3f052012-11-01 09:14:31 +0000374 return AP->getDataLayout().getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000375}
376
Bill Wendling47054f32009-05-15 00:11:17 +0000377#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000378void DIEDelta::printImpl(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000379 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000380}
381#endif
382
383//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000384// DIEString Implementation
385//===----------------------------------------------------------------------===//
386
387/// EmitValue - Emit string value.
388///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000389void DIEString::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000390 assert(
391 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
392 "Expected valid string form");
393
394 // Index of string in symbol table.
395 if (Form == dwarf::DW_FORM_GNU_str_index) {
396 DIEInteger(S.getIndex()).EmitValue(AP, Form);
397 return;
398 }
399
400 // Relocatable symbol.
401 assert(Form == dwarf::DW_FORM_strp);
402 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
403 DIELabel(S.getSymbol()).EmitValue(AP, Form);
404 return;
405 }
406
407 // Offset into symbol table.
408 DIEInteger(S.getOffset()).EmitValue(AP, Form);
Eric Christopher67646432013-07-26 17:02:41 +0000409}
410
411/// SizeOf - Determine size of delta value in bytes.
412///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000413unsigned DIEString::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000414 assert(
415 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
416 "Expected valid string form");
417
418 // Index of string in symbol table.
419 if (Form == dwarf::DW_FORM_GNU_str_index)
420 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
421
422 // Relocatable symbol.
423 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
424 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
425
426 // Offset into symbol table.
427 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
Eric Christopher67646432013-07-26 17:02:41 +0000428}
429
430#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000431void DIEString::printImpl(raw_ostream &O) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000432 O << "String: " << S.getString();
Eric Christopher67646432013-07-26 17:02:41 +0000433}
434#endif
435
436//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000437// DIEEntry Implementation
438//===----------------------------------------------------------------------===//
439
440/// EmitValue - Emit debug information entry offset.
441///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000442void DIEEntry::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000443
444 if (Form == dwarf::DW_FORM_ref_addr) {
445 const DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000446 unsigned Addr = Entry.getOffset();
Eric Christopherdd508382014-03-06 00:00:56 +0000447 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
448 // For DW_FORM_ref_addr, output the offset from beginning of debug info
449 // section. Entry->getOffset() returns the offset from start of the
450 // compile unit.
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000451 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
Eric Christopherdd508382014-03-06 00:00:56 +0000452 assert(CU && "CUDie should belong to a CU.");
453 Addr += CU->getDebugInfoOffset();
454 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
455 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
456 DIEEntry::getRefAddrSize(AP));
457 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000458 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
Eric Christopherdd508382014-03-06 00:00:56 +0000459 } else
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000460 AP->EmitInt32(Entry.getOffset());
Bill Wendling47054f32009-05-15 00:11:17 +0000461}
462
Frederic Risscd044342015-03-04 02:30:08 +0000463unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
Manman Renac8062b2013-07-02 23:40:10 +0000464 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
465 // specified to be four bytes in the DWARF 32-bit format and eight bytes
466 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
467 // references have the same size as an address on the target system.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000468 const DwarfDebug *DD = AP->getDwarfDebug();
469 assert(DD && "Expected Dwarf Debug info to be available");
470 if (DD->getDwarfVersion() == 2)
Manman Renac8062b2013-07-02 23:40:10 +0000471 return AP->getDataLayout().getPointerSize();
472 return sizeof(int32_t);
473}
474
Bill Wendling47054f32009-05-15 00:11:17 +0000475#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000476void DIEEntry::printImpl(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000477 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000478}
479#endif
480
481//===----------------------------------------------------------------------===//
David Blaikie47f615e2013-12-17 23:32:35 +0000482// DIETypeSignature Implementation
483//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000484void DIETypeSignature::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
David Blaikie47f615e2013-12-17 23:32:35 +0000485 assert(Form == dwarf::DW_FORM_ref_sig8);
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000486 Asm->OutStreamer->EmitIntValue(Unit.getTypeSignature(), 8);
David Blaikie47f615e2013-12-17 23:32:35 +0000487}
488
489#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000490void DIETypeSignature::printImpl(raw_ostream &O) const {
491 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
David Blaikie47f615e2013-12-17 23:32:35 +0000492}
David Blaikie47f615e2013-12-17 23:32:35 +0000493#endif
494
495//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000496// DIELoc Implementation
497//===----------------------------------------------------------------------===//
498
499/// ComputeSize - calculate the size of the location expression.
500///
Frederic Risscd044342015-03-04 02:30:08 +0000501unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000502 if (!Size) {
503 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
504 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000505 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
Eric Christopher8bdab432014-02-27 18:36:10 +0000506 }
Eric Christopher4a741042014-02-16 08:46:55 +0000507
Eric Christopher8bdab432014-02-27 18:36:10 +0000508 return Size;
Eric Christopher4a741042014-02-16 08:46:55 +0000509}
510
511/// EmitValue - Emit location data.
512///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000513void DIELoc::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000514 switch (Form) {
515 default: llvm_unreachable("Improper form for block");
516 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
517 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
518 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
519 case dwarf::DW_FORM_block:
520 case dwarf::DW_FORM_exprloc:
521 Asm->EmitULEB128(Size); break;
522 }
523
524 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
525 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000526 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
Eric Christopher4a741042014-02-16 08:46:55 +0000527}
528
529/// SizeOf - Determine size of location data in bytes.
530///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000531unsigned DIELoc::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000532 switch (Form) {
533 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
534 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
535 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
536 case dwarf::DW_FORM_block:
537 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000538 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000539 default: llvm_unreachable("Improper form for block");
540 }
541}
542
543#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000544void DIELoc::printImpl(raw_ostream &O) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000545 O << "ExprLoc: ";
546 DIE::print(O, 5);
547}
548#endif
549
550//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000551// DIEBlock Implementation
552//===----------------------------------------------------------------------===//
553
554/// ComputeSize - calculate the size of the block.
555///
Frederic Risscd044342015-03-04 02:30:08 +0000556unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000557 if (!Size) {
558 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
559 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000560 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
Eric Christopher8bdab432014-02-27 18:36:10 +0000561 }
Bill Wendling47054f32009-05-15 00:11:17 +0000562
Eric Christopher8bdab432014-02-27 18:36:10 +0000563 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000564}
565
566/// EmitValue - Emit block data.
567///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000568void DIEBlock::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000569 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000570 default: llvm_unreachable("Improper form for block");
Chris Lattner9efd1182010-04-04 19:09:29 +0000571 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
572 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
573 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
574 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Bill Wendling47054f32009-05-15 00:11:17 +0000575 }
576
Eric Christopher4887c8f2013-03-29 23:34:06 +0000577 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Chris Lattner3d72a672010-03-09 23:38:23 +0000578 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000579 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
Bill Wendling47054f32009-05-15 00:11:17 +0000580}
581
582/// SizeOf - Determine size of block data in bytes.
583///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000584unsigned DIEBlock::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000585 switch (Form) {
586 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
587 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
588 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000589 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
David Blaikie46a9f012012-01-20 21:51:11 +0000590 default: llvm_unreachable("Improper form for block");
Bill Wendling47054f32009-05-15 00:11:17 +0000591 }
Bill Wendling47054f32009-05-15 00:11:17 +0000592}
593
Bill Wendling47054f32009-05-15 00:11:17 +0000594#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000595void DIEBlock::printImpl(raw_ostream &O) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000596 O << "Blk: ";
597 DIE::print(O, 5);
598}
599#endif
Eric Christophera27220f2014-03-05 22:41:20 +0000600
601//===----------------------------------------------------------------------===//
602// DIELocList Implementation
603//===----------------------------------------------------------------------===//
604
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000605unsigned DIELocList::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christophera27220f2014-03-05 22:41:20 +0000606 if (Form == dwarf::DW_FORM_data4)
607 return 4;
608 if (Form == dwarf::DW_FORM_sec_offset)
609 return 4;
610 return AP->getDataLayout().getPointerSize();
611}
612
613/// EmitValue - Emit label value.
614///
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000615void DIELocList::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie9c550ac2014-03-25 01:44:02 +0000616 DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000617 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
Eric Christophera27220f2014-03-05 22:41:20 +0000618
David Blaikiee12ab122014-04-01 16:09:49 +0000619 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
Rafael Espindola063d7252015-03-10 16:58:10 +0000620 AP->emitSectionOffset(Label);
Eric Christophera27220f2014-03-05 22:41:20 +0000621 else
Rafael Espindola063d7252015-03-10 16:58:10 +0000622 AP->EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
Eric Christophera27220f2014-03-05 22:41:20 +0000623}
624
625#ifndef NDEBUG
Duncan P. N. Exon Smith583bc032015-05-27 19:30:27 +0000626void DIELocList::printImpl(raw_ostream &O) const {
627 O << "LocList: " << Index;
628
629}
Eric Christophera27220f2014-03-05 22:41:20 +0000630#endif