blob: 25dec5893c68fe8aaf39e4bc061b3659bb80618a [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
14#include "DIE.h"
David Blaikie37c52312014-10-04 15:49:50 +000015
16#include "DwarfCompileUnit.h"
Manman Renac8062b2013-07-02 23:40:10 +000017#include "DwarfDebug.h"
David Blaikie47f615e2013-12-17 23:32:35 +000018#include "DwarfUnit.h"
Benjamin Kramer4d128a22010-01-17 07:46:39 +000019#include "llvm/ADT/Twine.h"
Bill Wendling47054f32009-05-15 00:11:17 +000020#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000022#include "llvm/MC/MCAsmInfo.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"
Bill Wendling47054f32009-05-15 00:11:17 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// DIEAbbrevData Implementation
35//===----------------------------------------------------------------------===//
36
37/// Profile - Used to gather unique data for the abbreviation folding set.
38///
39void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000040 // Explicitly cast to an integer type for which FoldingSetNodeID has
41 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
42 ID.AddInteger(unsigned(Attribute));
43 ID.AddInteger(unsigned(Form));
Bill Wendling47054f32009-05-15 00:11:17 +000044}
45
46//===----------------------------------------------------------------------===//
47// DIEAbbrev Implementation
48//===----------------------------------------------------------------------===//
49
50/// Profile - Used to gather unique data for the abbreviation folding set.
51///
52void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
Reid Klecknerad65f102013-10-21 19:18:31 +000053 ID.AddInteger(unsigned(Tag));
Eric Christophere8f10722014-03-05 01:44:58 +000054 ID.AddInteger(unsigned(Children));
Bill Wendling47054f32009-05-15 00:11:17 +000055
56 // For each attribute description.
57 for (unsigned i = 0, N = Data.size(); i < N; ++i)
58 Data[i].Profile(ID);
59}
60
61/// Emit - Print the abbreviation using the specified asm printer.
62///
Chris Lattner3a383cb2010-04-05 00:13:49 +000063void DIEAbbrev::Emit(AsmPrinter *AP) const {
Bill Wendling47054f32009-05-15 00:11:17 +000064 // Emit its Dwarf tag type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000065 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
Bill Wendling47054f32009-05-15 00:11:17 +000066
67 // Emit whether it has children DIEs.
Eric Christophere8f10722014-03-05 01:44:58 +000068 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
Bill Wendling47054f32009-05-15 00:11:17 +000069
70 // For each attribute description.
71 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
72 const DIEAbbrevData &AttrData = Data[i];
73
74 // Emit attribute type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000075 AP->EmitULEB128(AttrData.getAttribute(),
Nick Lewycky019d2552011-07-29 03:49:23 +000076 dwarf::AttributeString(AttrData.getAttribute()));
Bill Wendling47054f32009-05-15 00:11:17 +000077
78 // Emit form type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000079 AP->EmitULEB128(AttrData.getForm(),
80 dwarf::FormEncodingString(AttrData.getForm()));
Bill Wendling47054f32009-05-15 00:11:17 +000081 }
82
83 // Mark end of abbreviation.
Chris Lattner3a383cb2010-04-05 00:13:49 +000084 AP->EmitULEB128(0, "EOM(1)");
85 AP->EmitULEB128(0, "EOM(2)");
Bill Wendling47054f32009-05-15 00:11:17 +000086}
87
88#ifndef NDEBUG
Chris Lattner74725712009-08-23 01:01:17 +000089void DIEAbbrev::print(raw_ostream &O) {
Bill Wendling47054f32009-05-15 00:11:17 +000090 O << "Abbreviation @"
Chris Lattner74725712009-08-23 01:01:17 +000091 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling47054f32009-05-15 00:11:17 +000092 << " "
93 << dwarf::TagString(Tag)
94 << " "
Eric Christophere8f10722014-03-05 01:44:58 +000095 << dwarf::ChildrenString(Children)
Chris Lattner74725712009-08-23 01:01:17 +000096 << '\n';
Bill Wendling47054f32009-05-15 00:11:17 +000097
98 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
99 O << " "
100 << dwarf::AttributeString(Data[i].getAttribute())
101 << " "
102 << dwarf::FormEncodingString(Data[i].getForm())
Chris Lattner74725712009-08-23 01:01:17 +0000103 << '\n';
Bill Wendling47054f32009-05-15 00:11:17 +0000104 }
105}
David Greene8bc072c2009-12-24 00:27:55 +0000106void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling47054f32009-05-15 00:11:17 +0000107#endif
108
Eric Christopherd89221e2013-11-21 01:01:30 +0000109/// Climb up the parent chain to get the unit DIE to which this DIE
Manman Rence20d462013-10-29 22:57:10 +0000110/// belongs.
David Blaikie409dd9c2013-11-19 23:08:21 +0000111const DIE *DIE::getUnit() const {
112 const DIE *Cu = getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000113 assert(Cu && "We should not have orphaned DIEs.");
114 return Cu;
115}
116
Eric Christopherd89221e2013-11-21 01:01:30 +0000117/// Climb up the parent chain to get the unit DIE this DIE belongs
Manman Ren4dbdc902013-10-31 17:54:35 +0000118/// to. Return NULL if DIE is not added to an owner yet.
David Blaikie409dd9c2013-11-19 23:08:21 +0000119const DIE *DIE::getUnitOrNull() const {
Manman Rence20d462013-10-29 22:57:10 +0000120 const DIE *p = this;
121 while (p) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000122 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
123 p->getTag() == dwarf::DW_TAG_type_unit)
Manman Rence20d462013-10-29 22:57:10 +0000124 return p;
125 p = p->getParent();
126 }
Craig Topper353eda42014-04-24 06:44:33 +0000127 return nullptr;
Manman Rence20d462013-10-29 22:57:10 +0000128}
129
Eric Christopher0af53e22014-03-05 01:10:59 +0000130DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
Eric Christopher8552e222013-08-07 01:18:33 +0000131 const SmallVectorImpl<DIEValue *> &Values = getValues();
132 const DIEAbbrev &Abbrevs = getAbbrev();
133
134 // Iterate through all the attributes until we find the one we're
135 // looking for, if we can't find it return NULL.
136 for (size_t i = 0; i < Values.size(); ++i)
137 if (Abbrevs.getData()[i].getAttribute() == Attribute)
138 return Values[i];
Craig Topper353eda42014-04-24 06:44:33 +0000139 return nullptr;
Eric Christopher8552e222013-08-07 01:18:33 +0000140}
141
Bill Wendling47054f32009-05-15 00:11:17 +0000142#ifndef NDEBUG
Eric Christopher6c6de842013-05-06 17:50:50 +0000143void DIE::print(raw_ostream &O, unsigned IndentCount) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000144 const std::string Indent(IndentCount, ' ');
145 bool isBlock = Abbrev.getTag() == 0;
146
147 if (!isBlock) {
148 O << Indent
149 << "Die: "
Chris Lattner74725712009-08-23 01:01:17 +0000150 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling47054f32009-05-15 00:11:17 +0000151 << ", Offset: " << Offset
Chris Lattner3d72a672010-03-09 23:38:23 +0000152 << ", Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000153
154 O << Indent
155 << dwarf::TagString(Abbrev.getTag())
156 << " "
Eric Christophere8f10722014-03-05 01:44:58 +0000157 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000158 } else {
Chris Lattner3d72a672010-03-09 23:38:23 +0000159 O << "Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000160 }
Bill Wendling47054f32009-05-15 00:11:17 +0000161
Eric Christopher4887c8f2013-03-29 23:34:06 +0000162 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
Bill Wendling47054f32009-05-15 00:11:17 +0000163
164 IndentCount += 2;
165 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
166 O << Indent;
167
168 if (!isBlock)
169 O << dwarf::AttributeString(Data[i].getAttribute());
170 else
171 O << "Blk[" << i << "]";
172
173 O << " "
174 << dwarf::FormEncodingString(Data[i].getForm())
175 << " ";
176 Values[i]->print(O);
177 O << "\n";
178 }
179 IndentCount -= 2;
180
181 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
Eric Christopher6c6de842013-05-06 17:50:50 +0000182 Children[j]->print(O, IndentCount+4);
Bill Wendling47054f32009-05-15 00:11:17 +0000183 }
184
185 if (!isBlock) O << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000186}
187
188void DIE::dump() {
David Greene8bc072c2009-12-24 00:27:55 +0000189 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000190}
191#endif
192
David Blaikiea379b1812011-12-20 02:50:00 +0000193void DIEValue::anchor() { }
Bill Wendling47054f32009-05-15 00:11:17 +0000194
195#ifndef NDEBUG
Eric Christopher65ac02a2013-05-31 22:50:40 +0000196void DIEValue::dump() const {
David Greene8bc072c2009-12-24 00:27:55 +0000197 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000198}
199#endif
200
201//===----------------------------------------------------------------------===//
202// DIEInteger Implementation
203//===----------------------------------------------------------------------===//
204
205/// EmitValue - Emit integer of appropriate size.
206///
David Blaikief2443192013-10-21 17:28:37 +0000207void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
Chris Lattner71601e82010-01-20 07:41:15 +0000208 unsigned Size = ~0U;
Bill Wendling47054f32009-05-15 00:11:17 +0000209 switch (Form) {
Eric Christopherbb69a272012-08-24 01:14:27 +0000210 case dwarf::DW_FORM_flag_present:
211 // Emit something to keep the lines and comments in sync.
212 // FIXME: Is there a better way to do this?
Rafael Espindola74c3e632014-01-16 07:36:00 +0000213 Asm->OutStreamer.AddBlankLine();
Eric Christopherbb69a272012-08-24 01:14:27 +0000214 return;
Bill Wendling47054f32009-05-15 00:11:17 +0000215 case dwarf::DW_FORM_flag: // Fall thru
216 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000217 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling47054f32009-05-15 00:11:17 +0000218 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000219 case dwarf::DW_FORM_data2: Size = 2; break;
Eric Christopher18266172013-01-17 02:59:59 +0000220 case dwarf::DW_FORM_sec_offset: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000221 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000222 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling47054f32009-05-15 00:11:17 +0000223 case dwarf::DW_FORM_ref8: // Fall thru
David Blaikie409dd9c2013-11-19 23:08:21 +0000224 case dwarf::DW_FORM_ref_sig8: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000225 case dwarf::DW_FORM_data8: Size = 8; break;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000226 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
Eric Christopher962c9082013-01-15 23:56:56 +0000227 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
Chris Lattner9efd1182010-04-04 19:09:29 +0000228 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
229 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
Eric Christophere8a7b1b2012-09-10 23:34:03 +0000230 case dwarf::DW_FORM_addr:
Chandler Carruth5da3f052012-11-01 09:14:31 +0000231 Size = Asm->getDataLayout().getPointerSize(); break;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000232 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000233 }
Eric Christopherce0cfce2013-01-09 01:35:34 +0000234 Asm->OutStreamer.EmitIntValue(Integer, Size);
Bill Wendling47054f32009-05-15 00:11:17 +0000235}
236
237/// SizeOf - Determine size of integer value in bytes.
238///
David Blaikief2443192013-10-21 17:28:37 +0000239unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000240 switch (Form) {
Eric Christopher2a4e6162012-08-29 17:59:32 +0000241 case dwarf::DW_FORM_flag_present: return 0;
Bill Wendling47054f32009-05-15 00:11:17 +0000242 case dwarf::DW_FORM_flag: // Fall thru
243 case dwarf::DW_FORM_ref1: // Fall thru
244 case dwarf::DW_FORM_data1: return sizeof(int8_t);
245 case dwarf::DW_FORM_ref2: // Fall thru
246 case dwarf::DW_FORM_data2: return sizeof(int16_t);
Eric Christopher18266172013-01-17 02:59:59 +0000247 case dwarf::DW_FORM_sec_offset: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000248 case dwarf::DW_FORM_ref4: // Fall thru
249 case dwarf::DW_FORM_data4: return sizeof(int32_t);
250 case dwarf::DW_FORM_ref8: // Fall thru
David Blaikie409dd9c2013-11-19 23:08:21 +0000251 case dwarf::DW_FORM_ref_sig8: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000252 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000253 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
254 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
255 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
256 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
Chandler Carruth5da3f052012-11-01 09:14:31 +0000257 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
David Blaikie46a9f012012-01-20 21:51:11 +0000258 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000259 }
Bill Wendling47054f32009-05-15 00:11:17 +0000260}
261
Bill Wendling47054f32009-05-15 00:11:17 +0000262#ifndef NDEBUG
Eric Christopher65ac02a2013-05-31 22:50:40 +0000263void DIEInteger::print(raw_ostream &O) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000264 O << "Int: " << (int64_t)Integer << " 0x";
265 O.write_hex(Integer);
Bill Wendling47054f32009-05-15 00:11:17 +0000266}
267#endif
268
269//===----------------------------------------------------------------------===//
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000270// DIEExpr Implementation
271//===----------------------------------------------------------------------===//
272
273/// EmitValue - Emit expression value.
274///
David Blaikief2443192013-10-21 17:28:37 +0000275void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000276 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
277}
278
279/// SizeOf - Determine size of expression value in bytes.
280///
David Blaikief2443192013-10-21 17:28:37 +0000281unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000282 if (Form == dwarf::DW_FORM_data4) return 4;
283 if (Form == dwarf::DW_FORM_sec_offset) return 4;
284 if (Form == dwarf::DW_FORM_strp) return 4;
285 return AP->getDataLayout().getPointerSize();
286}
287
288#ifndef NDEBUG
289void DIEExpr::print(raw_ostream &O) const {
290 O << "Expr: ";
291 Expr->print(O);
292}
293#endif
294
295//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000296// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000297//===----------------------------------------------------------------------===//
298
299/// EmitValue - Emit label value.
300///
David Blaikief2443192013-10-21 17:28:37 +0000301void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
302 AP->EmitLabelReference(Label, SizeOf(AP, Form),
303 Form == dwarf::DW_FORM_strp ||
304 Form == dwarf::DW_FORM_sec_offset ||
305 Form == dwarf::DW_FORM_ref_addr);
Bill Wendling47054f32009-05-15 00:11:17 +0000306}
307
308/// SizeOf - Determine size of label value in bytes.
309///
David Blaikief2443192013-10-21 17:28:37 +0000310unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000311 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher4c7765f2013-01-17 03:00:04 +0000312 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000313 if (Form == dwarf::DW_FORM_strp) return 4;
Chandler Carruth5da3f052012-11-01 09:14:31 +0000314 return AP->getDataLayout().getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000315}
316
Bill Wendling47054f32009-05-15 00:11:17 +0000317#ifndef NDEBUG
Eric Christopher65ac02a2013-05-31 22:50:40 +0000318void DIELabel::print(raw_ostream &O) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000319 O << "Lbl: " << Label->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000320}
321#endif
322
323//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000324// DIEDelta Implementation
325//===----------------------------------------------------------------------===//
326
327/// EmitValue - Emit delta value.
328///
David Blaikief2443192013-10-21 17:28:37 +0000329void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000330 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000331}
332
333/// SizeOf - Determine size of delta value in bytes.
334///
David Blaikief2443192013-10-21 17:28:37 +0000335unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000336 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher33ff6972013-11-21 23:46:41 +0000337 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000338 if (Form == dwarf::DW_FORM_strp) return 4;
Chandler Carruth5da3f052012-11-01 09:14:31 +0000339 return AP->getDataLayout().getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000340}
341
Bill Wendling47054f32009-05-15 00:11:17 +0000342#ifndef NDEBUG
Eric Christopher65ac02a2013-05-31 22:50:40 +0000343void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000344 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000345}
346#endif
347
348//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000349// DIEString Implementation
350//===----------------------------------------------------------------------===//
351
352/// EmitValue - Emit string value.
353///
David Blaikief2443192013-10-21 17:28:37 +0000354void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher67646432013-07-26 17:02:41 +0000355 Access->EmitValue(AP, Form);
356}
357
358/// SizeOf - Determine size of delta value in bytes.
359///
David Blaikief2443192013-10-21 17:28:37 +0000360unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher67646432013-07-26 17:02:41 +0000361 return Access->SizeOf(AP, Form);
362}
363
364#ifndef NDEBUG
365void DIEString::print(raw_ostream &O) const {
366 O << "String: " << Str << "\tSymbol: ";
367 Access->print(O);
368}
369#endif
370
371//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000372// DIEEntry Implementation
373//===----------------------------------------------------------------------===//
374
375/// EmitValue - Emit debug information entry offset.
376///
David Blaikief2443192013-10-21 17:28:37 +0000377void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000378
379 if (Form == dwarf::DW_FORM_ref_addr) {
380 const DwarfDebug *DD = AP->getDwarfDebug();
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000381 unsigned Addr = Entry.getOffset();
Eric Christopherdd508382014-03-06 00:00:56 +0000382 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
383 // For DW_FORM_ref_addr, output the offset from beginning of debug info
384 // section. Entry->getOffset() returns the offset from start of the
385 // compile unit.
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000386 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
Eric Christopherdd508382014-03-06 00:00:56 +0000387 assert(CU && "CUDie should belong to a CU.");
388 Addr += CU->getDebugInfoOffset();
389 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
390 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
391 DIEEntry::getRefAddrSize(AP));
392 else
393 AP->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
394 CU->getSectionSym(),
395 DIEEntry::getRefAddrSize(AP));
396 } else
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000397 AP->EmitInt32(Entry.getOffset());
Bill Wendling47054f32009-05-15 00:11:17 +0000398}
399
Manman Renac8062b2013-07-02 23:40:10 +0000400unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
401 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
402 // specified to be four bytes in the DWARF 32-bit format and eight bytes
403 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
404 // references have the same size as an address on the target system.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000405 const DwarfDebug *DD = AP->getDwarfDebug();
406 assert(DD && "Expected Dwarf Debug info to be available");
407 if (DD->getDwarfVersion() == 2)
Manman Renac8062b2013-07-02 23:40:10 +0000408 return AP->getDataLayout().getPointerSize();
409 return sizeof(int32_t);
410}
411
Bill Wendling47054f32009-05-15 00:11:17 +0000412#ifndef NDEBUG
Eric Christopher65ac02a2013-05-31 22:50:40 +0000413void DIEEntry::print(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000414 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000415}
416#endif
417
418//===----------------------------------------------------------------------===//
David Blaikie47f615e2013-12-17 23:32:35 +0000419// DIETypeSignature Implementation
420//===----------------------------------------------------------------------===//
421void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
422 assert(Form == dwarf::DW_FORM_ref_sig8);
423 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
424}
425
426#ifndef NDEBUG
427void DIETypeSignature::print(raw_ostream &O) const {
428 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
429}
430
431void DIETypeSignature::dump() const { print(dbgs()); }
432#endif
433
434//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000435// DIELoc Implementation
436//===----------------------------------------------------------------------===//
437
438/// ComputeSize - calculate the size of the location expression.
439///
Eric Christopher5d503b52014-02-20 02:40:45 +0000440unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000441 if (!Size) {
442 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
443 for (unsigned i = 0, N = Values.size(); i < N; ++i)
444 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
445 }
Eric Christopher4a741042014-02-16 08:46:55 +0000446
Eric Christopher8bdab432014-02-27 18:36:10 +0000447 return Size;
Eric Christopher4a741042014-02-16 08:46:55 +0000448}
449
450/// EmitValue - Emit location data.
451///
452void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
453 switch (Form) {
454 default: llvm_unreachable("Improper form for block");
455 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
456 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
457 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
458 case dwarf::DW_FORM_block:
459 case dwarf::DW_FORM_exprloc:
460 Asm->EmitULEB128(Size); break;
461 }
462
463 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
464 for (unsigned i = 0, N = Values.size(); i < N; ++i)
465 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
466}
467
468/// SizeOf - Determine size of location data in bytes.
469///
470unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
471 switch (Form) {
472 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
473 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
474 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
475 case dwarf::DW_FORM_block:
476 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000477 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000478 default: llvm_unreachable("Improper form for block");
479 }
480}
481
482#ifndef NDEBUG
483void DIELoc::print(raw_ostream &O) const {
484 O << "ExprLoc: ";
485 DIE::print(O, 5);
486}
487#endif
488
489//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000490// DIEBlock Implementation
491//===----------------------------------------------------------------------===//
492
493/// ComputeSize - calculate the size of the block.
494///
Eric Christopher5d503b52014-02-20 02:40:45 +0000495unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000496 if (!Size) {
497 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
498 for (unsigned i = 0, N = Values.size(); i < N; ++i)
499 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
500 }
Bill Wendling47054f32009-05-15 00:11:17 +0000501
Eric Christopher8bdab432014-02-27 18:36:10 +0000502 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000503}
504
505/// EmitValue - Emit block data.
506///
David Blaikief2443192013-10-21 17:28:37 +0000507void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000508 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000509 default: llvm_unreachable("Improper form for block");
Chris Lattner9efd1182010-04-04 19:09:29 +0000510 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
511 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
512 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
513 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Bill Wendling47054f32009-05-15 00:11:17 +0000514 }
515
Eric Christopher4887c8f2013-03-29 23:34:06 +0000516 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Chris Lattner3d72a672010-03-09 23:38:23 +0000517 for (unsigned i = 0, N = Values.size(); i < N; ++i)
Chris Lattner3a383cb2010-04-05 00:13:49 +0000518 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
Bill Wendling47054f32009-05-15 00:11:17 +0000519}
520
521/// SizeOf - Determine size of block data in bytes.
522///
David Blaikief2443192013-10-21 17:28:37 +0000523unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000524 switch (Form) {
525 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
526 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
527 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000528 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
David Blaikie46a9f012012-01-20 21:51:11 +0000529 default: llvm_unreachable("Improper form for block");
Bill Wendling47054f32009-05-15 00:11:17 +0000530 }
Bill Wendling47054f32009-05-15 00:11:17 +0000531}
532
Bill Wendling47054f32009-05-15 00:11:17 +0000533#ifndef NDEBUG
Eric Christopher65ac02a2013-05-31 22:50:40 +0000534void DIEBlock::print(raw_ostream &O) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000535 O << "Blk: ";
536 DIE::print(O, 5);
537}
538#endif
Eric Christophera27220f2014-03-05 22:41:20 +0000539
540//===----------------------------------------------------------------------===//
541// DIELocList Implementation
542//===----------------------------------------------------------------------===//
543
544unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
545 if (Form == dwarf::DW_FORM_data4)
546 return 4;
547 if (Form == dwarf::DW_FORM_sec_offset)
548 return 4;
549 return AP->getDataLayout().getPointerSize();
550}
551
552/// EmitValue - Emit label value.
553///
554void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie9c550ac2014-03-25 01:44:02 +0000555 DwarfDebug *DD = AP->getDwarfDebug();
David Blaikie0a456de2014-04-02 01:43:18 +0000556 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
Eric Christophera27220f2014-03-05 22:41:20 +0000557
David Blaikiee12ab122014-04-01 16:09:49 +0000558 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
David Blaikie9c550ac2014-03-25 01:44:02 +0000559 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
Eric Christophera27220f2014-03-05 22:41:20 +0000560 else
David Blaikie9c550ac2014-03-25 01:44:02 +0000561 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
Eric Christophera27220f2014-03-05 22:41:20 +0000562}
563
564#ifndef NDEBUG
565void DIELocList::print(raw_ostream &O) const {
566 O << "LocList: " << Index;
567
568}
569#endif