blob: 6e9834a7cf0e556f89bbf30b7a6b3e5f2d956acf [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.
Mehdi Amini149f6ea2016-10-05 05:59:29 +000066 AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
Bill Wendling47054f32009-05-15 00:11:17 +000067
68 // Emit whether it has children DIEs.
Mehdi Amini149f6ea2016-10-05 05:59:29 +000069 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
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(),
Mehdi Amini149f6ea2016-10-05 05:59:29 +000077 dwarf::AttributeString(AttrData.getAttribute()).data());
Bill Wendling47054f32009-05-15 00:11:17 +000078
79 // Emit form type.
Chris Lattner3a383cb2010-04-05 00:13:49 +000080 AP->EmitULEB128(AttrData.getForm(),
Mehdi Amini149f6ea2016-10-05 05:59:29 +000081 dwarf::FormEncodingString(AttrData.getForm()).data());
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
Davide Italianoc304a0d2015-11-24 02:21:43 +000089LLVM_DUMP_METHOD
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}
Davide Italianoc304a0d2015-11-24 02:21:43 +0000107
108LLVM_DUMP_METHOD
David Greene8bc072c2009-12-24 00:27:55 +0000109void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling47054f32009-05-15 00:11:17 +0000110
Greg Clayton35630c32016-12-01 18:56:29 +0000111DIE *DIE::getParent() const {
112 return Owner.dyn_cast<DIE*>();
113}
114
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000115DIEAbbrev DIE::generateAbbrev() const {
116 DIEAbbrev Abbrev(Tag, hasChildren());
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000117 for (const DIEValue &V : values())
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000118 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
119 return Abbrev;
120}
121
Greg Clayton35630c32016-12-01 18:56:29 +0000122unsigned DIE::getDebugSectionOffset() const {
123 const DIEUnit *Unit = getUnit();
124 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
125 return getUnit()->getDebugSectionOffset() + getOffset();
Manman Ren4dbdc902013-10-31 17:54:35 +0000126}
127
Greg Clayton35630c32016-12-01 18:56:29 +0000128const DIE *DIE::getUnitDie() const {
Manman Rence20d462013-10-29 22:57:10 +0000129 const DIE *p = this;
130 while (p) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000131 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
132 p->getTag() == dwarf::DW_TAG_type_unit)
Manman Rence20d462013-10-29 22:57:10 +0000133 return p;
134 p = p->getParent();
135 }
Craig Topper353eda42014-04-24 06:44:33 +0000136 return nullptr;
Manman Rence20d462013-10-29 22:57:10 +0000137}
138
Greg Clayton35630c32016-12-01 18:56:29 +0000139const DIEUnit *DIE::getUnit() const {
140 const DIE *UnitDie = getUnitDie();
141 if (UnitDie)
142 return UnitDie->Owner.dyn_cast<DIEUnit*>();
143 return nullptr;
144}
145
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000146DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
Eric Christopher8552e222013-08-07 01:18:33 +0000147 // Iterate through all the attributes until we find the one we're
148 // looking for, if we can't find it return NULL.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000149 for (const auto &V : values())
150 if (V.getAttribute() == Attribute)
151 return V;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000152 return DIEValue();
Eric Christopher8552e222013-08-07 01:18:33 +0000153}
154
Davide Italianoc304a0d2015-11-24 02:21:43 +0000155LLVM_DUMP_METHOD
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000156static void printValues(raw_ostream &O, const DIEValueList &Values,
157 StringRef Type, unsigned Size, unsigned IndentCount) {
158 O << Type << ": Size: " << Size << "\n";
159
160 unsigned I = 0;
161 const std::string Indent(IndentCount, ' ');
162 for (const auto &V : Values.values()) {
163 O << Indent;
164 O << "Blk[" << I++ << "]";
165 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
166 V.print(O);
167 O << "\n";
168 }
169}
170
Davide Italianoc304a0d2015-11-24 02:21:43 +0000171LLVM_DUMP_METHOD
Eric Christopher6c6de842013-05-06 17:50:50 +0000172void DIE::print(raw_ostream &O, unsigned IndentCount) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000173 const std::string Indent(IndentCount, ' ');
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000174 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
175 << ", Offset: " << Offset << ", Size: " << Size << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000176
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000177 O << Indent << dwarf::TagString(getTag()) << " "
178 << dwarf::ChildrenString(hasChildren()) << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000179
Bill Wendling47054f32009-05-15 00:11:17 +0000180 IndentCount += 2;
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000181 for (const auto &V : values()) {
Bill Wendling47054f32009-05-15 00:11:17 +0000182 O << Indent;
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000183 O << dwarf::AttributeString(V.getAttribute());
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000184 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
185 V.print(O);
Bill Wendling47054f32009-05-15 00:11:17 +0000186 O << "\n";
187 }
188 IndentCount -= 2;
189
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000190 for (const auto &Child : children())
191 Child.print(O, IndentCount + 4);
Bill Wendling47054f32009-05-15 00:11:17 +0000192
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000193 O << "\n";
Bill Wendling47054f32009-05-15 00:11:17 +0000194}
195
Davide Italianoc304a0d2015-11-24 02:21:43 +0000196LLVM_DUMP_METHOD
Bill Wendling47054f32009-05-15 00:11:17 +0000197void DIE::dump() {
David Greene8bc072c2009-12-24 00:27:55 +0000198 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000199}
Bill Wendling47054f32009-05-15 00:11:17 +0000200
Greg Clayton35630c32016-12-01 18:56:29 +0000201DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
202 : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
203 AddrSize(A)
204{
205 Die.setUnit(this);
206 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
207 UnitTag == dwarf::DW_TAG_type_unit ||
208 UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
209}
210
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000211void DIEValue::EmitValue(const AsmPrinter *AP) const {
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000212 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000213 case isNone:
214 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000215#define HANDLE_DIEVALUE(T) \
216 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000217 getDIE##T().EmitValue(AP, Form); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000218 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000219#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000220 }
221}
222
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000223unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000224 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000225 case isNone:
226 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000227#define HANDLE_DIEVALUE(T) \
228 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000229 return getDIE##T().SizeOf(AP, Form);
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000230#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000231 }
Aaron Ballmanc681c3d2015-05-23 14:46:49 +0000232 llvm_unreachable("Unknown DIE kind");
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000233}
Bill Wendling47054f32009-05-15 00:11:17 +0000234
Davide Italianoc304a0d2015-11-24 02:21:43 +0000235LLVM_DUMP_METHOD
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000236void DIEValue::print(raw_ostream &O) const {
237 switch (Ty) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000238 case isNone:
239 llvm_unreachable("Expected valid DIEValue");
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000240#define HANDLE_DIEVALUE(T) \
241 case is##T: \
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000242 getDIE##T().print(O); \
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000243 break;
Duncan P. N. Exon Smithff189272015-05-27 21:15:43 +0000244#include "llvm/CodeGen/DIEValue.def"
Duncan P. N. Exon Smith68b3f302015-05-23 01:45:07 +0000245 }
246}
247
Davide Italianoc304a0d2015-11-24 02:21:43 +0000248LLVM_DUMP_METHOD
Eric Christopher65ac02a2013-05-31 22:50:40 +0000249void DIEValue::dump() const {
David Greene8bc072c2009-12-24 00:27:55 +0000250 print(dbgs());
Bill Wendling47054f32009-05-15 00:11:17 +0000251}
Bill Wendling47054f32009-05-15 00:11:17 +0000252
253//===----------------------------------------------------------------------===//
254// DIEInteger Implementation
255//===----------------------------------------------------------------------===//
256
257/// EmitValue - Emit integer of appropriate size.
258///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000259void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Chris Lattner71601e82010-01-20 07:41:15 +0000260 unsigned Size = ~0U;
Bill Wendling47054f32009-05-15 00:11:17 +0000261 switch (Form) {
Eric Christopherbb69a272012-08-24 01:14:27 +0000262 case dwarf::DW_FORM_flag_present:
263 // Emit something to keep the lines and comments in sync.
264 // FIXME: Is there a better way to do this?
Lang Hames9ff69c82015-04-24 19:11:51 +0000265 Asm->OutStreamer->AddBlankLine();
Eric Christopherbb69a272012-08-24 01:14:27 +0000266 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000267 case dwarf::DW_FORM_flag: LLVM_FALLTHROUGH;
268 case dwarf::DW_FORM_ref1: LLVM_FALLTHROUGH;
Chris Lattner71601e82010-01-20 07:41:15 +0000269 case dwarf::DW_FORM_data1: Size = 1; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000270 case dwarf::DW_FORM_ref2: LLVM_FALLTHROUGH;
Chris Lattner71601e82010-01-20 07:41:15 +0000271 case dwarf::DW_FORM_data2: Size = 2; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000272 case dwarf::DW_FORM_sec_offset: LLVM_FALLTHROUGH;
273 case dwarf::DW_FORM_strp: LLVM_FALLTHROUGH;
274 case dwarf::DW_FORM_ref4: LLVM_FALLTHROUGH;
Chris Lattner71601e82010-01-20 07:41:15 +0000275 case dwarf::DW_FORM_data4: Size = 4; break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000276 case dwarf::DW_FORM_ref8: LLVM_FALLTHROUGH;
277 case dwarf::DW_FORM_ref_sig8: LLVM_FALLTHROUGH;
Chris Lattner71601e82010-01-20 07:41:15 +0000278 case dwarf::DW_FORM_data8: Size = 8; break;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000279 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
Eric Christopher962c9082013-01-15 23:56:56 +0000280 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
Chris Lattner9efd1182010-04-04 19:09:29 +0000281 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
282 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
Eric Christophere8a7b1b2012-09-10 23:34:03 +0000283 case dwarf::DW_FORM_addr:
Mehdi Amini1660cab2015-07-16 05:59:25 +0000284 Size = Asm->getPointerSize();
285 break;
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000286 case dwarf::DW_FORM_ref_addr:
287 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
288 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +0000289 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000290 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000291 Asm->OutStreamer->EmitIntValue(Integer, Size);
Bill Wendling47054f32009-05-15 00:11:17 +0000292}
293
294/// SizeOf - Determine size of integer value in bytes.
295///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000296unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000297 switch (Form) {
Eric Christopher2a4e6162012-08-29 17:59:32 +0000298 case dwarf::DW_FORM_flag_present: return 0;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000299 case dwarf::DW_FORM_flag: LLVM_FALLTHROUGH;
300 case dwarf::DW_FORM_ref1: LLVM_FALLTHROUGH;
Bill Wendling47054f32009-05-15 00:11:17 +0000301 case dwarf::DW_FORM_data1: return sizeof(int8_t);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000302 case dwarf::DW_FORM_ref2: LLVM_FALLTHROUGH;
Bill Wendling47054f32009-05-15 00:11:17 +0000303 case dwarf::DW_FORM_data2: return sizeof(int16_t);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000304 case dwarf::DW_FORM_sec_offset: LLVM_FALLTHROUGH;
305 case dwarf::DW_FORM_strp: LLVM_FALLTHROUGH;
306 case dwarf::DW_FORM_ref4: LLVM_FALLTHROUGH;
Bill Wendling47054f32009-05-15 00:11:17 +0000307 case dwarf::DW_FORM_data4: return sizeof(int32_t);
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000308 case dwarf::DW_FORM_ref8: LLVM_FALLTHROUGH;
309 case dwarf::DW_FORM_ref_sig8: LLVM_FALLTHROUGH;
Bill Wendling47054f32009-05-15 00:11:17 +0000310 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000311 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
312 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
313 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
314 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
Mehdi Amini1660cab2015-07-16 05:59:25 +0000315 case dwarf::DW_FORM_addr:
316 return AP->getPointerSize();
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000317 case dwarf::DW_FORM_ref_addr:
Greg Claytone6543972016-11-23 23:30:37 +0000318 if (AP->getDwarfVersion() == 2)
Mehdi Amini1660cab2015-07-16 05:59:25 +0000319 return AP->getPointerSize();
Frederic Rissee17fb9b2015-03-04 22:07:36 +0000320 return sizeof(int32_t);
David Blaikie46a9f012012-01-20 21:51:11 +0000321 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling47054f32009-05-15 00:11:17 +0000322 }
Bill Wendling47054f32009-05-15 00:11:17 +0000323}
324
Davide Italianoc304a0d2015-11-24 02:21:43 +0000325LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000326void DIEInteger::print(raw_ostream &O) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000327 O << "Int: " << (int64_t)Integer << " 0x";
328 O.write_hex(Integer);
Bill Wendling47054f32009-05-15 00:11:17 +0000329}
Bill Wendling47054f32009-05-15 00:11:17 +0000330
331//===----------------------------------------------------------------------===//
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000332// DIEExpr Implementation
333//===----------------------------------------------------------------------===//
334
335/// EmitValue - Emit expression value.
336///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000337void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Lang Hames9ff69c82015-04-24 19:11:51 +0000338 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000339}
340
341/// SizeOf - Determine size of expression value in bytes.
342///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000343unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000344 if (Form == dwarf::DW_FORM_data4) return 4;
345 if (Form == dwarf::DW_FORM_sec_offset) return 4;
346 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000347 return AP->getPointerSize();
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000348}
349
Davide Italianoc304a0d2015-11-24 02:21:43 +0000350LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000351void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000352
353//===----------------------------------------------------------------------===//
Chris Lattner8dcf41e2010-03-08 22:31:46 +0000354// DIELabel Implementation
Bill Wendling47054f32009-05-15 00:11:17 +0000355//===----------------------------------------------------------------------===//
356
357/// EmitValue - Emit label value.
358///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000359void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikief2443192013-10-21 17:28:37 +0000360 AP->EmitLabelReference(Label, SizeOf(AP, Form),
361 Form == dwarf::DW_FORM_strp ||
362 Form == dwarf::DW_FORM_sec_offset ||
363 Form == dwarf::DW_FORM_ref_addr);
Bill Wendling47054f32009-05-15 00:11:17 +0000364}
365
366/// SizeOf - Determine size of label value in bytes.
367///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000368unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000369 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher4c7765f2013-01-17 03:00:04 +0000370 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000371 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000372 return AP->getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000373}
374
Davide Italianoc304a0d2015-11-24 02:21:43 +0000375LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000376void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
Bill Wendling47054f32009-05-15 00:11:17 +0000377
378//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000379// DIEDelta Implementation
380//===----------------------------------------------------------------------===//
381
382/// EmitValue - Emit delta value.
383///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000384void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000385 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
Bill Wendling47054f32009-05-15 00:11:17 +0000386}
387
388/// SizeOf - Determine size of delta value in bytes.
389///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000390unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000391 if (Form == dwarf::DW_FORM_data4) return 4;
Eric Christopher33ff6972013-11-21 23:46:41 +0000392 if (Form == dwarf::DW_FORM_sec_offset) return 4;
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000393 if (Form == dwarf::DW_FORM_strp) return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000394 return AP->getPointerSize();
Bill Wendling47054f32009-05-15 00:11:17 +0000395}
396
Davide Italianoc304a0d2015-11-24 02:21:43 +0000397LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000398void DIEDelta::print(raw_ostream &O) const {
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000399 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
Bill Wendling47054f32009-05-15 00:11:17 +0000400}
Bill Wendling47054f32009-05-15 00:11:17 +0000401
402//===----------------------------------------------------------------------===//
Eric Christopher67646432013-07-26 17:02:41 +0000403// DIEString Implementation
404//===----------------------------------------------------------------------===//
405
406/// EmitValue - Emit string value.
407///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000408void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000409 assert(
410 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
411 "Expected valid string form");
412
413 // Index of string in symbol table.
414 if (Form == dwarf::DW_FORM_GNU_str_index) {
415 DIEInteger(S.getIndex()).EmitValue(AP, Form);
416 return;
417 }
418
419 // Relocatable symbol.
420 assert(Form == dwarf::DW_FORM_strp);
421 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
422 DIELabel(S.getSymbol()).EmitValue(AP, Form);
423 return;
424 }
425
426 // Offset into symbol table.
427 DIEInteger(S.getOffset()).EmitValue(AP, Form);
Eric Christopher67646432013-07-26 17:02:41 +0000428}
429
430/// SizeOf - Determine size of delta value in bytes.
431///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000432unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000433 assert(
434 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
435 "Expected valid string form");
436
437 // Index of string in symbol table.
438 if (Form == dwarf::DW_FORM_GNU_str_index)
439 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
440
441 // Relocatable symbol.
442 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
443 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
444
445 // Offset into symbol table.
446 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
Eric Christopher67646432013-07-26 17:02:41 +0000447}
448
Davide Italianoc304a0d2015-11-24 02:21:43 +0000449LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000450void DIEString::print(raw_ostream &O) const {
Duncan P. N. Exon Smithf73bcf42015-05-24 16:40:47 +0000451 O << "String: " << S.getString();
Eric Christopher67646432013-07-26 17:02:41 +0000452}
Eric Christopher67646432013-07-26 17:02:41 +0000453
454//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000455// DIEEntry Implementation
456//===----------------------------------------------------------------------===//
457
458/// EmitValue - Emit debug information entry offset.
459///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000460void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopherdd508382014-03-06 00:00:56 +0000461
462 if (Form == dwarf::DW_FORM_ref_addr) {
Greg Clayton35630c32016-12-01 18:56:29 +0000463 // Get the absolute offset for this DIE within the debug info/types section.
464 unsigned Addr = Entry->getDebugSectionOffset();
465 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
466 const DwarfDebug *DD = AP->getDwarfDebug();
467 if (DD)
468 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
469 const DIEUnit *Unit = Entry->getUnit();
470 assert(Unit && "CUDie should belong to a CU.");
471 MCSection *Section = Unit->getSection();
472 assert(Section && "Must have a section if we are doing relocations");
473 const MCSymbol *SectionSym = Section->getBeginSymbol();
474 AP->EmitLabelPlusOffset(SectionSym, Addr, DIEEntry::getRefAddrSize(AP));
475 } else
Lang Hames9ff69c82015-04-24 19:11:51 +0000476 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
Eric Christopherdd508382014-03-06 00:00:56 +0000477 } else
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000478 AP->EmitInt32(Entry->getOffset());
Bill Wendling47054f32009-05-15 00:11:17 +0000479}
480
Frederic Risscd044342015-03-04 02:30:08 +0000481unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
Manman Renac8062b2013-07-02 23:40:10 +0000482 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
483 // specified to be four bytes in the DWARF 32-bit format and eight bytes
484 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
485 // references have the same size as an address on the target system.
Greg Claytone6543972016-11-23 23:30:37 +0000486 if (AP->getDwarfVersion() == 2)
Mehdi Amini1660cab2015-07-16 05:59:25 +0000487 return AP->getPointerSize();
Manman Renac8062b2013-07-02 23:40:10 +0000488 return sizeof(int32_t);
489}
490
Davide Italianoc304a0d2015-11-24 02:21:43 +0000491LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000492void DIEEntry::print(raw_ostream &O) const {
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000493 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
Bill Wendling47054f32009-05-15 00:11:17 +0000494}
Bill Wendling47054f32009-05-15 00:11:17 +0000495
496//===----------------------------------------------------------------------===//
Eric Christopher4a741042014-02-16 08:46:55 +0000497// DIELoc Implementation
498//===----------------------------------------------------------------------===//
499
500/// ComputeSize - calculate the size of the location expression.
501///
Frederic Risscd044342015-03-04 02:30:08 +0000502unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000503 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000504 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000505 Size += V.SizeOf(AP);
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 Smithe7e1d0c2015-05-27 22:14:58 +0000513void DIELoc::EmitValue(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
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000524 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000525 V.EmitValue(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000526}
527
528/// SizeOf - Determine size of location data in bytes.
529///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000530unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christopher4a741042014-02-16 08:46:55 +0000531 switch (Form) {
532 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
533 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
534 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
535 case dwarf::DW_FORM_block:
536 case dwarf::DW_FORM_exprloc:
Logan Chien5b776b72014-02-22 14:00:39 +0000537 return Size + getULEB128Size(Size);
Eric Christopher4a741042014-02-16 08:46:55 +0000538 default: llvm_unreachable("Improper form for block");
539 }
540}
541
Davide Italianoc304a0d2015-11-24 02:21:43 +0000542LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000543void DIELoc::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000544 printValues(O, *this, "ExprLoc", Size, 5);
Eric Christopher4a741042014-02-16 08:46:55 +0000545}
Eric Christopher4a741042014-02-16 08:46:55 +0000546
547//===----------------------------------------------------------------------===//
Bill Wendling47054f32009-05-15 00:11:17 +0000548// DIEBlock Implementation
549//===----------------------------------------------------------------------===//
550
551/// ComputeSize - calculate the size of the block.
552///
Frederic Risscd044342015-03-04 02:30:08 +0000553unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
Eric Christopher8bdab432014-02-27 18:36:10 +0000554 if (!Size) {
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000555 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000556 Size += V.SizeOf(AP);
Eric Christopher8bdab432014-02-27 18:36:10 +0000557 }
Bill Wendling47054f32009-05-15 00:11:17 +0000558
Eric Christopher8bdab432014-02-27 18:36:10 +0000559 return Size;
Bill Wendling47054f32009-05-15 00:11:17 +0000560}
561
562/// EmitValue - Emit block data.
563///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000564void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000565 switch (Form) {
Craig Topperee4dab52012-02-05 08:31:47 +0000566 default: llvm_unreachable("Improper form for block");
Chris Lattner9efd1182010-04-04 19:09:29 +0000567 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
568 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
569 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
570 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
Bill Wendling47054f32009-05-15 00:11:17 +0000571 }
572
Duncan P. N. Exon Smith1ad5ebc2015-08-02 20:42:45 +0000573 for (const auto &V : values())
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000574 V.EmitValue(Asm);
Bill Wendling47054f32009-05-15 00:11:17 +0000575}
576
577/// SizeOf - Determine size of block data in bytes.
578///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000579unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Bill Wendling47054f32009-05-15 00:11:17 +0000580 switch (Form) {
581 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
582 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
583 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Logan Chien5b776b72014-02-22 14:00:39 +0000584 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
David Blaikie46a9f012012-01-20 21:51:11 +0000585 default: llvm_unreachable("Improper form for block");
Bill Wendling47054f32009-05-15 00:11:17 +0000586 }
Bill Wendling47054f32009-05-15 00:11:17 +0000587}
588
Davide Italianoc304a0d2015-11-24 02:21:43 +0000589LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000590void DIEBlock::print(raw_ostream &O) const {
Duncan P. N. Exon Smithc5821142015-08-02 20:46:49 +0000591 printValues(O, *this, "Blk", Size, 5);
Bill Wendling47054f32009-05-15 00:11:17 +0000592}
Eric Christophera27220f2014-03-05 22:41:20 +0000593
594//===----------------------------------------------------------------------===//
595// DIELocList Implementation
596//===----------------------------------------------------------------------===//
597
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000598unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
Eric Christophera27220f2014-03-05 22:41:20 +0000599 if (Form == dwarf::DW_FORM_data4)
600 return 4;
601 if (Form == dwarf::DW_FORM_sec_offset)
602 return 4;
Mehdi Amini1660cab2015-07-16 05:59:25 +0000603 return AP->getPointerSize();
Eric Christophera27220f2014-03-05 22:41:20 +0000604}
605
606/// EmitValue - Emit label value.
607///
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000608void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
David Blaikie9c550ac2014-03-25 01:44:02 +0000609 DwarfDebug *DD = AP->getDwarfDebug();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000610 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
Rafael Espindola857546e2015-06-16 23:22:02 +0000611 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
Eric Christophera27220f2014-03-05 22:41:20 +0000612}
613
Davide Italianoc304a0d2015-11-24 02:21:43 +0000614LLVM_DUMP_METHOD
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000615void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }