blob: 41790bd58bcbbba92702329f22a1e26845aab43b [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
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000110DIEAbbrev DIE::generateAbbrev() const {
111 DIEAbbrev Abbrev(Tag, hasChildren());
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000112 for (const DIEValue &V : values())
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000113 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
114 return Abbrev;
115}
116
Eric Christopherd89221e2013-11-21 01:01:30 +0000117/// Climb up the parent chain to get the unit DIE to which this DIE
Manman Rence20d462013-10-29 22:57:10 +0000118/// belongs.
David Blaikie409dd9c2013-11-19 23:08:21 +0000119const DIE *DIE::getUnit() const {
120 const DIE *Cu = getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000121 assert(Cu && "We should not have orphaned DIEs.");
122 return Cu;
123}
124
Eric Christopherd89221e2013-11-21 01:01:30 +0000125/// Climb up the parent chain to get the unit DIE this DIE belongs
Manman Ren4dbdc902013-10-31 17:54:35 +0000126/// to. Return NULL if DIE is not added to an owner yet.
David Blaikie409dd9c2013-11-19 23:08:21 +0000127const DIE *DIE::getUnitOrNull() const {
Manman Rence20d462013-10-29 22:57:10 +0000128 const DIE *p = this;
129 while (p) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000130 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
131 p->getTag() == dwarf::DW_TAG_type_unit)
Manman Rence20d462013-10-29 22:57:10 +0000132 return p;
133 p = p->getParent();
134 }
Craig Topper353eda42014-04-24 06:44:33 +0000135 return nullptr;
Manman Rence20d462013-10-29 22:57:10 +0000136}
137
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000138DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
Eric Christopher8552e222013-08-07 01:18:33 +0000139 // Iterate through all the attributes until we find the one we're
140 // looking for, if we can't find it return NULL.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000141 for (const auto &V : values())
142 if (V.getAttribute() == Attribute)
143 return V;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000144 return DIEValue();
Eric Christopher8552e222013-08-07 01:18:33 +0000145}
146
Bill Wendling47054f32009-05-15 00:11:17 +0000147#ifndef NDEBUG
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000148static void printValues(raw_ostream &O, const DIEValueList &Values,
149 StringRef Type, unsigned Size, unsigned IndentCount) {
150 O << Type << ": Size: " << Size << "\n";
151
152 unsigned I = 0;
153 const std::string Indent(IndentCount, ' ');
154 for (const auto &V : Values.values()) {
155 O << Indent;
156 O << "Blk[" << I++ << "]";
157 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
158 V.print(O);
159 O << "\n";
160 }
161}
162
Eric Christopher6c6de842013-05-06 17:50:50 +0000163void DIE::print(raw_ostream &O, unsigned IndentCount) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000164 const std::string Indent(IndentCount, ' ');
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000165 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
166 << ", Offset: " << Offset << ", Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000167
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000168 O << Indent << dwarf::TagString(getTag()) << " "
169 << dwarf::ChildrenString(hasChildren()) << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000170
Bill Wendling47054f32009-05-15 00:11:17 +0000171 IndentCount += 2;
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000172 for (const auto &V : values()) {
Bill Wendling47054f32009-05-15 00:11:17 +0000173 O << Indent;
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000174 O << dwarf::AttributeString(V.getAttribute());
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000175 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
176 V.print(O);
Bill Wendling47054f32009-05-15 00:11:17 +0000177 O << "\n";
178 }
179 IndentCount -= 2;
180
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000181 for (const auto &Child : children())
182 Child.print(O, IndentCount + 4);
Bill Wendling47054f32009-05-15 00:11:17 +0000183
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000184 O << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000185}
186
187void DIE::dump() {
David Greene8bc072c2009-12-24 00:27:55 +0000188 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000189}
190#endif
191
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000192void DIEValue::EmitValue(const AsmPrinter *AP) const {
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000193 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000194 case isNone:
195 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000196#define HANDLE_DIEVALUE(T) \
197 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000198 getDIE##T().EmitValue(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
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000204unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000205 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000206 case isNone:
207 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000208#define HANDLE_DIEVALUE(T) \
209 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000210 return getDIE##T().SizeOf(AP, Form);
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000211#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000212 }
Aaron Ballmanc681c3d2015-05-23 14:46:49 +0000213 llvm_unreachable("Unknown DIE kind");
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000214}
Bill Wendling47054f32009-05-15 00:11:17 +0000215
216#ifndef NDEBUG
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000217void DIEValue::print(raw_ostream &O) const {
218 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000219 case isNone:
220 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000221#define HANDLE_DIEVALUE(T) \
222 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000223 getDIE##T().print(O); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000224 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000225#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000226 }
227}
228
Eric Christopher65ac02a2013-05-31 22:50:40 +0000229void DIEValue::dump() const {
David Greene8bc072c2009-12-24 00:27:55 +0000230 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000231}
232#endif
233
234//===----------------------------------------------------------------------===//
235// DIEInteger Implementation
236//===----------------------------------------------------------------------===//
237
238/// EmitValue - Emit integer of appropriate size.
239///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000240void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Chris Lattner71601e82010-01-20 07:41:15 +0000241 unsigned Size = ~0U;
Bill Wendling47054f32009-05-15 00:11:17 +0000242 switch (Form) {
Eric Christopherbb69a272012-08-24 01:14:27 +0000243 case dwarf::DW_FORM_flag_present:
244 // Emit something to keep the lines and comments in sync.
245 // FIXME: Is there a better way to do this?
Lang Hames9ff69c82015-04-24 19:11:51 +0000246 Asm->OutStreamer->AddBlankLine();
Eric Christopherbb69a272012-08-24 01:14:27 +0000247 return;
Bill Wendling47054f32009-05-15 00:11:17 +0000248 case dwarf::DW_FORM_flag: // Fall thru
249 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000250 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling47054f32009-05-15 00:11:17 +0000251 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000252 case dwarf::DW_FORM_data2: Size = 2; break;
Eric Christopher18266172013-01-17 02:59:59 +0000253 case dwarf::DW_FORM_sec_offset: // Fall thru
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000254 case dwarf::DW_FORM_strp: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000255 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000256 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling47054f32009-05-15 00:11:17 +0000257 case dwarf::DW_FORM_ref8: // Fall thru
David Blaikie409dd9c2013-11-19 23:08:21 +0000258 case dwarf::DW_FORM_ref_sig8: // Fall thru
Chris Lattner71601e82010-01-20 07:41:15 +0000259 case dwarf::DW_FORM_data8: Size = 8; break;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000260 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
Eric Christopher962c9082013-01-15 23:56:56 +0000261 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
Chris Lattner9efd1182010-04-04 19:09:29 +0000262 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
263 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
Eric Christophere8a7b1b2012-09-10 23:34:03 +0000264 case dwarf::DW_FORM_addr:
Mehdi Amini1660cab2015-07-16 05:59:25 +0000265 Size = Asm->getPointerSize();
266 break;
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000267 case dwarf::DW_FORM_ref_addr:
268 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
269 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000270 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000271 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000272 Asm->OutStreamer->EmitIntValue(Integer, Size);
Bill Wendling47054f32009-05-15 00:11:17 +0000273}
274
275/// SizeOf - Determine size of integer value in bytes.
276///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000277unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000278 switch (Form) {
Eric Christopher2a4e6162012-08-29 17:59:32 +0000279 case dwarf::DW_FORM_flag_present: return 0;
Bill Wendling47054f32009-05-15 00:11:17 +0000280 case dwarf::DW_FORM_flag: // Fall thru
281 case dwarf::DW_FORM_ref1: // Fall thru
282 case dwarf::DW_FORM_data1: return sizeof(int8_t);
283 case dwarf::DW_FORM_ref2: // Fall thru
284 case dwarf::DW_FORM_data2: return sizeof(int16_t);
Eric Christopher18266172013-01-17 02:59:59 +0000285 case dwarf::DW_FORM_sec_offset: // Fall thru
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000286 case dwarf::DW_FORM_strp: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000287 case dwarf::DW_FORM_ref4: // Fall thru
288 case dwarf::DW_FORM_data4: return sizeof(int32_t);
289 case dwarf::DW_FORM_ref8: // Fall thru
David Blaikie409dd9c2013-11-19 23:08:21 +0000290 case dwarf::DW_FORM_ref_sig8: // Fall thru
Bill Wendling47054f32009-05-15 00:11:17 +0000291 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000292 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
293 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
294 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
295 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
Mehdi Amini1660cab2015-07-16 05:59:25 +0000296 case dwarf::DW_FORM_addr:
297 return AP->getPointerSize();
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000298 case dwarf::DW_FORM_ref_addr:
Lang Hames9ff69c82015-04-24 19:11:51 +0000299 if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
Mehdi Amini1660cab2015-07-16 05:59:25 +0000300 return AP->getPointerSize();
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000301 return sizeof(int32_t);
David Blaikie46a9f012012-01-20 21:51:11 +0000302 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000303 }
Bill Wendling47054f32009-05-15 00:11:17 +0000304}
305
Bill Wendling47054f32009-05-15 00:11:17 +0000306#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000307void DIEInteger::print(raw_ostream &O) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000308 O << "Int: " << (int64_t)Integer << " 0x";
309 O.write_hex(Integer);
Bill Wendling47054f32009-05-15 00:11:17 +0000310}
311#endif
312
313//===----------------------------------------------------------------------===//
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000314// DIEExpr Implementation
315//===----------------------------------------------------------------------===//
316
317/// EmitValue - Emit expression value.
318///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000319void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000320 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000321}
322
323/// SizeOf - Determine size of expression value in bytes.
324///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000325unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000326 if (Form == dwarf::DW_FORM_data4) return 4;
327 if (Form == dwarf::DW_FORM_sec_offset) return 4;
328 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000329 return AP->getPointerSize();
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000330}
331
332#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000333void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000334#endif
335
336//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000337// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000338//===----------------------------------------------------------------------===//
339
340/// EmitValue - Emit label value.
341///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000342void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikief2443192013-10-21 17:28:37 +0000343 AP->EmitLabelReference(Label, SizeOf(AP, Form),
344 Form == dwarf::DW_FORM_strp ||
345 Form == dwarf::DW_FORM_sec_offset ||
346 Form == dwarf::DW_FORM_ref_addr);
Bill Wendling47054f32009-05-15 00:11:17 +0000347}
348
349/// SizeOf - Determine size of label value in bytes.
350///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000351unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000352 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher4c7765f2013-01-17 03:00:04 +0000353 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000354 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000355 return AP->getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000356}
357
Bill Wendling47054f32009-05-15 00:11:17 +0000358#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000359void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
Bill Wendling47054f32009-05-15 00:11:17 +0000360#endif
361
362//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000363// DIEDelta Implementation
364//===----------------------------------------------------------------------===//
365
366/// EmitValue - Emit delta value.
367///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000368void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000369 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000370}
371
372/// SizeOf - Determine size of delta value in bytes.
373///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000374unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000375 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher33ff6972013-11-21 23:46:41 +0000376 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000377 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000378 return AP->getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000379}
380
Bill Wendling47054f32009-05-15 00:11:17 +0000381#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000382void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000383 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000384}
385#endif
386
387//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000388// DIEString Implementation
389//===----------------------------------------------------------------------===//
390
391/// EmitValue - Emit string value.
392///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000393void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000394 assert(
395 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
396 "Expected valid string form");
397
398 // Index of string in symbol table.
399 if (Form == dwarf::DW_FORM_GNU_str_index) {
400 DIEInteger(S.getIndex()).EmitValue(AP, Form);
401 return;
402 }
403
404 // Relocatable symbol.
405 assert(Form == dwarf::DW_FORM_strp);
406 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
407 DIELabel(S.getSymbol()).EmitValue(AP, Form);
408 return;
409 }
410
411 // Offset into symbol table.
412 DIEInteger(S.getOffset()).EmitValue(AP, Form);
Eric Christopher67646432013-07-26 17:02:41 +0000413}
414
415/// SizeOf - Determine size of delta value in bytes.
416///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000417unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000418 assert(
419 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
420 "Expected valid string form");
421
422 // Index of string in symbol table.
423 if (Form == dwarf::DW_FORM_GNU_str_index)
424 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
425
426 // Relocatable symbol.
427 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
428 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
429
430 // Offset into symbol table.
431 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
Eric Christopher67646432013-07-26 17:02:41 +0000432}
433
434#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000435void DIEString::print(raw_ostream &O) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000436 O << "String: " << S.getString();
Eric Christopher67646432013-07-26 17:02:41 +0000437}
438#endif
439
440//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000441// DIEEntry Implementation
442//===----------------------------------------------------------------------===//
443
444/// EmitValue - Emit debug information entry offset.
445///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000446void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000447
448 if (Form == dwarf::DW_FORM_ref_addr) {
449 const DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000450 unsigned Addr = Entry->getOffset();
Eric Christopherdd508382014-03-06 00:00:56 +0000451 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
452 // For DW_FORM_ref_addr, output the offset from beginning of debug info
453 // section. Entry->getOffset() returns the offset from start of the
454 // compile unit.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000455 DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
Eric Christopherdd508382014-03-06 00:00:56 +0000456 assert(CU && "CUDie should belong to a CU.");
457 Addr += CU->getDebugInfoOffset();
458 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
459 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
460 DIEEntry::getRefAddrSize(AP));
461 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000462 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
Eric Christopherdd508382014-03-06 00:00:56 +0000463 } else
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000464 AP->EmitInt32(Entry->getOffset());
Bill Wendling47054f32009-05-15 00:11:17 +0000465}
466
Frederic Risscd044342015-03-04 02:30:08 +0000467unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
Manman Renac8062b2013-07-02 23:40:10 +0000468 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
469 // specified to be four bytes in the DWARF 32-bit format and eight bytes
470 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
471 // references have the same size as an address on the target system.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000472 const DwarfDebug *DD = AP->getDwarfDebug();
473 assert(DD && "Expected Dwarf Debug info to be available");
474 if (DD->getDwarfVersion() == 2)
Mehdi Amini1660cab2015-07-16 05:59:25 +0000475 return AP->getPointerSize();
Manman Renac8062b2013-07-02 23:40:10 +0000476 return sizeof(int32_t);
477}
478
Bill Wendling47054f32009-05-15 00:11:17 +0000479#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000480void DIEEntry::print(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000481 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000482}
483#endif
484
485//===----------------------------------------------------------------------===//
David Blaikie47f615e2013-12-17 23:32:35 +0000486// DIETypeSignature Implementation
487//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000488void DIETypeSignature::EmitValue(const AsmPrinter *Asm,
489 dwarf::Form Form) const {
David Blaikie47f615e2013-12-17 23:32:35 +0000490 assert(Form == dwarf::DW_FORM_ref_sig8);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000491 Asm->OutStreamer->EmitIntValue(Unit->getTypeSignature(), 8);
David Blaikie47f615e2013-12-17 23:32:35 +0000492}
493
494#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000495void DIETypeSignature::print(raw_ostream &O) const {
496 O << format("Type Unit: 0x%lx", Unit->getTypeSignature());
David Blaikie47f615e2013-12-17 23:32:35 +0000497}
David Blaikie47f615e2013-12-17 23:32:35 +0000498#endif
499
500//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000501// DIELoc Implementation
502//===----------------------------------------------------------------------===//
503
504/// ComputeSize - calculate the size of the location expression.
505///
Frederic Risscd044342015-03-04 02:30:08 +0000506unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000507 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000508 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000509 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000510 }
Eric Christopher4a741042014-02-16 08:46:55 +0000511
Eric Christopher8bdab432014-02-27 18:36:10 +0000512 return Size;
Eric Christopher4a741042014-02-16 08:46:55 +0000513}
514
515/// EmitValue - Emit location data.
516///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000517void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000518 switch (Form) {
519 default: llvm_unreachable("Improper form for block");
520 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
521 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
522 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
523 case dwarf::DW_FORM_block:
524 case dwarf::DW_FORM_exprloc:
525 Asm->EmitULEB128(Size); break;
526 }
527
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000528 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000529 V.EmitValue(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000530}
531
532/// SizeOf - Determine size of location data in bytes.
533///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000534unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000535 switch (Form) {
536 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
537 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
538 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
539 case dwarf::DW_FORM_block:
540 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000541 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000542 default: llvm_unreachable("Improper form for block");
543 }
544}
545
546#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000547void DIELoc::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000548 printValues(O, *this, "ExprLoc", Size, 5);
Eric Christopher4a741042014-02-16 08:46:55 +0000549}
550#endif
551
552//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000553// DIEBlock Implementation
554//===----------------------------------------------------------------------===//
555
556/// ComputeSize - calculate the size of the block.
557///
Frederic Risscd044342015-03-04 02:30:08 +0000558unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000559 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000560 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000561 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000562 }
Bill Wendling47054f32009-05-15 00:11:17 +0000563
Eric Christopher8bdab432014-02-27 18:36:10 +0000564 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000565}
566
567/// EmitValue - Emit block data.
568///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000569void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000570 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000571 default: llvm_unreachable("Improper form for block");
Chris Lattner9efd1182010-04-04 19:09:29 +0000572 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
573 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
574 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
575 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Bill Wendling47054f32009-05-15 00:11:17 +0000576 }
577
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000578 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000579 V.EmitValue(Asm);
Bill Wendling47054f32009-05-15 00:11:17 +0000580}
581
582/// SizeOf - Determine size of block data in bytes.
583///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000584unsigned DIEBlock::SizeOf(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 Smithe7e1d0c2015-05-27 22:14:58 +0000595void DIEBlock::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000596 printValues(O, *this, "Blk", Size, 5);
Bill Wendling47054f32009-05-15 00:11:17 +0000597}
598#endif
Eric Christophera27220f2014-03-05 22:41:20 +0000599
600//===----------------------------------------------------------------------===//
601// DIELocList Implementation
602//===----------------------------------------------------------------------===//
603
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000604unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christophera27220f2014-03-05 22:41:20 +0000605 if (Form == dwarf::DW_FORM_data4)
606 return 4;
607 if (Form == dwarf::DW_FORM_sec_offset)
608 return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000609 return AP->getPointerSize();
Eric Christophera27220f2014-03-05 22:41:20 +0000610}
611
612/// EmitValue - Emit label value.
613///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000614void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie9c550ac2014-03-25 01:44:02 +0000615 DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000616 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
Rafael Espindola857546e2015-06-16 23:22:02 +0000617 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
Eric Christophera27220f2014-03-05 22:41:20 +0000618}
619
620#ifndef NDEBUG
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000621void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
Eric Christophera27220f2014-03-05 22:41:20 +0000622#endif