blob: 349e0ac40f1edfb109749b241ee0d2bbecadcf70 [file] [log] [blame]
Bill Wendling88423ee2009-05-15 00:11:17 +00001//===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Data structures for DWARF info entries.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DIE.h"
15#include "DwarfPrinter.h"
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +000016#include "llvm/ADT/Twine.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000017#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerbcb83e52010-01-20 07:41:15 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattner858431d2010-01-16 18:50:28 +000020#include "llvm/MC/MCSymbol.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000021#include "llvm/Target/TargetData.h"
David Greene0c8b6e62009-12-24 00:27:55 +000022#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000024#include "llvm/Support/Format.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000025#include "llvm/Support/FormattedStream.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// DIEAbbrevData Implementation
30//===----------------------------------------------------------------------===//
31
32/// Profile - Used to gather unique data for the abbreviation folding set.
33///
34void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
35 ID.AddInteger(Attribute);
36 ID.AddInteger(Form);
37}
38
39//===----------------------------------------------------------------------===//
40// DIEAbbrev Implementation
41//===----------------------------------------------------------------------===//
42
43/// Profile - Used to gather unique data for the abbreviation folding set.
44///
45void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
46 ID.AddInteger(Tag);
47 ID.AddInteger(ChildrenFlag);
48
49 // For each attribute description.
50 for (unsigned i = 0, N = Data.size(); i < N; ++i)
51 Data[i].Profile(ID);
52}
53
54/// Emit - Print the abbreviation using the specified asm printer.
55///
Chris Lattner894d75a2010-01-22 23:18:42 +000056void DIEAbbrev::Emit(const DwarfPrinter *DP) const {
Bill Wendling88423ee2009-05-15 00:11:17 +000057 // Emit its Dwarf tag type.
Chris Lattner894d75a2010-01-22 23:18:42 +000058 // FIXME: Doing work even in non-asm-verbose runs.
59 DP->EmitULEB128(Tag, dwarf::TagString(Tag));
Bill Wendling88423ee2009-05-15 00:11:17 +000060
61 // Emit whether it has children DIEs.
Chris Lattner894d75a2010-01-22 23:18:42 +000062 // FIXME: Doing work even in non-asm-verbose runs.
63 DP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
Bill Wendling88423ee2009-05-15 00:11:17 +000064
65 // For each attribute description.
66 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
67 const DIEAbbrevData &AttrData = Data[i];
68
69 // Emit attribute type.
Chris Lattner894d75a2010-01-22 23:18:42 +000070 // FIXME: Doing work even in non-asm-verbose runs.
71 DP->EmitULEB128(AttrData.getAttribute(),
72 dwarf::AttributeString(AttrData.getAttribute()));
Bill Wendling88423ee2009-05-15 00:11:17 +000073
74 // Emit form type.
Chris Lattner894d75a2010-01-22 23:18:42 +000075 // FIXME: Doing work even in non-asm-verbose runs.
76 DP->EmitULEB128(AttrData.getForm(),
77 dwarf::FormEncodingString(AttrData.getForm()));
Bill Wendling88423ee2009-05-15 00:11:17 +000078 }
79
80 // Mark end of abbreviation.
Chris Lattner894d75a2010-01-22 23:18:42 +000081 DP->EmitULEB128(0, "EOM(1)");
82 DP->EmitULEB128(0, "EOM(2)");
Bill Wendling88423ee2009-05-15 00:11:17 +000083}
84
85#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +000086void DIEAbbrev::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +000087 O << "Abbreviation @"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000088 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +000089 << " "
90 << dwarf::TagString(Tag)
91 << " "
92 << dwarf::ChildrenString(ChildrenFlag)
Chris Lattnerb01acfa2009-08-23 01:01:17 +000093 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +000094
95 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
96 O << " "
97 << dwarf::AttributeString(Data[i].getAttribute())
98 << " "
99 << dwarf::FormEncodingString(Data[i].getForm())
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000100 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000101 }
102}
David Greene0c8b6e62009-12-24 00:27:55 +0000103void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling88423ee2009-05-15 00:11:17 +0000104#endif
105
106//===----------------------------------------------------------------------===//
107// DIE Implementation
108//===----------------------------------------------------------------------===//
109
110DIE::~DIE() {
111 for (unsigned i = 0, N = Children.size(); i < N; ++i)
112 delete Children[i];
113}
114
Devang Patel2c4ceb12009-11-21 02:48:08 +0000115/// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000116///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000117void DIE::addSiblingOffset() {
Bill Wendling88423ee2009-05-15 00:11:17 +0000118 DIEInteger *DI = new DIEInteger(0);
119 Values.insert(Values.begin(), DI);
120 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
121}
122
Bill Wendling88423ee2009-05-15 00:11:17 +0000123#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000124void DIE::print(raw_ostream &O, unsigned IncIndent) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000125 IndentCount += IncIndent;
126 const std::string Indent(IndentCount, ' ');
127 bool isBlock = Abbrev.getTag() == 0;
128
129 if (!isBlock) {
130 O << Indent
131 << "Die: "
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000132 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +0000133 << ", Offset: " << Offset
134 << ", Size: " << Size
135 << "\n";
136
137 O << Indent
138 << dwarf::TagString(Abbrev.getTag())
139 << " "
140 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
141 } else {
142 O << "Size: " << Size;
143 }
144 O << "\n";
145
146 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
147
148 IndentCount += 2;
149 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
150 O << Indent;
151
152 if (!isBlock)
153 O << dwarf::AttributeString(Data[i].getAttribute());
154 else
155 O << "Blk[" << i << "]";
156
157 O << " "
158 << dwarf::FormEncodingString(Data[i].getForm())
159 << " ";
160 Values[i]->print(O);
161 O << "\n";
162 }
163 IndentCount -= 2;
164
165 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
166 Children[j]->print(O, 4);
167 }
168
169 if (!isBlock) O << "\n";
170 IndentCount -= IncIndent;
171}
172
173void DIE::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000174 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000175}
176#endif
177
178
179#ifndef NDEBUG
180void DIEValue::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000181 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000182}
183#endif
184
185//===----------------------------------------------------------------------===//
186// DIEInteger Implementation
187//===----------------------------------------------------------------------===//
188
189/// EmitValue - Emit integer of appropriate size.
190///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000191void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000192 const AsmPrinter *Asm = D->getAsm();
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000193 unsigned Size = ~0U;
Bill Wendling88423ee2009-05-15 00:11:17 +0000194 switch (Form) {
195 case dwarf::DW_FORM_flag: // Fall thru
196 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000197 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000198 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000199 case dwarf::DW_FORM_data2: Size = 2; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000200 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000201 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000202 case dwarf::DW_FORM_ref8: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000203 case dwarf::DW_FORM_data8: Size = 8; break;
Chris Lattner894d75a2010-01-22 23:18:42 +0000204 case dwarf::DW_FORM_udata: D->EmitULEB128(Integer); return;
Chris Lattnerbb9078a2010-01-22 22:56:55 +0000205 case dwarf::DW_FORM_sdata: D->EmitSLEB128(Integer, ""); return;
Torok Edwinc23197a2009-07-14 16:55:14 +0000206 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling88423ee2009-05-15 00:11:17 +0000207 }
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000208 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
Bill Wendling88423ee2009-05-15 00:11:17 +0000209}
210
211/// SizeOf - Determine size of integer value in bytes.
212///
213unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
214 switch (Form) {
215 case dwarf::DW_FORM_flag: // Fall thru
216 case dwarf::DW_FORM_ref1: // Fall thru
217 case dwarf::DW_FORM_data1: return sizeof(int8_t);
218 case dwarf::DW_FORM_ref2: // Fall thru
219 case dwarf::DW_FORM_data2: return sizeof(int16_t);
220 case dwarf::DW_FORM_ref4: // Fall thru
221 case dwarf::DW_FORM_data4: return sizeof(int32_t);
222 case dwarf::DW_FORM_ref8: // Fall thru
223 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Chris Lattneraf76e592009-08-22 20:48:53 +0000224 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
225 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
Torok Edwinc23197a2009-07-14 16:55:14 +0000226 default: llvm_unreachable("DIE Value form not supported yet"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000227 }
228 return 0;
229}
230
Bill Wendling88423ee2009-05-15 00:11:17 +0000231#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000232void DIEInteger::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000233 O << "Int: " << (int64_t)Integer
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000234 << format(" 0x%llx", (unsigned long long)Integer);
Bill Wendling88423ee2009-05-15 00:11:17 +0000235}
236#endif
237
238//===----------------------------------------------------------------------===//
239// DIEString Implementation
240//===----------------------------------------------------------------------===//
241
242/// EmitValue - Emit string value.
243///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000244void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const {
Chris Lattner4cf202b2010-01-23 03:11:46 +0000245 D->getAsm()->OutStreamer.EmitBytes(Str, /*addrspace*/0);
246 // Emit nul terminator.
247 D->getAsm()->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0);
Bill Wendling88423ee2009-05-15 00:11:17 +0000248}
249
Bill Wendling88423ee2009-05-15 00:11:17 +0000250#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000251void DIEString::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000252 O << "Str: \"" << Str << "\"";
253}
254#endif
255
256//===----------------------------------------------------------------------===//
257// DIEDwarfLabel Implementation
258//===----------------------------------------------------------------------===//
259
260/// EmitValue - Emit label value.
261///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000262void DIEDwarfLabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000263 bool IsSmall = Form == dwarf::DW_FORM_data4;
264 D->EmitReference(Label, false, IsSmall);
265}
266
267/// SizeOf - Determine size of label value in bytes.
268///
269unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
270 if (Form == dwarf::DW_FORM_data4) return 4;
271 return TD->getPointerSize();
272}
273
Bill Wendling88423ee2009-05-15 00:11:17 +0000274#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000275void DIEDwarfLabel::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000276 O << "Lbl: ";
277 Label.print(O);
278}
279#endif
280
281//===----------------------------------------------------------------------===//
282// DIEObjectLabel Implementation
283//===----------------------------------------------------------------------===//
284
285/// EmitValue - Emit label value.
286///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000287void DIEObjectLabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000288 bool IsSmall = Form == dwarf::DW_FORM_data4;
Chris Lattner858431d2010-01-16 18:50:28 +0000289 D->EmitReference(Sym, false, IsSmall);
Bill Wendling88423ee2009-05-15 00:11:17 +0000290}
291
292/// SizeOf - Determine size of label value in bytes.
293///
294unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
295 if (Form == dwarf::DW_FORM_data4) return 4;
296 return TD->getPointerSize();
297}
298
Bill Wendling88423ee2009-05-15 00:11:17 +0000299#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000300void DIEObjectLabel::print(raw_ostream &O) {
Chris Lattner858431d2010-01-16 18:50:28 +0000301 O << "Obj: " << Sym->getName();
Bill Wendling88423ee2009-05-15 00:11:17 +0000302}
303#endif
304
305//===----------------------------------------------------------------------===//
306// DIESectionOffset Implementation
307//===----------------------------------------------------------------------===//
308
309/// EmitValue - Emit delta value.
310///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000311void DIESectionOffset::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000312 bool IsSmall = Form == dwarf::DW_FORM_data4;
313 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
314 Label.getNumber(), Section.getNumber(),
315 IsSmall, IsEH, UseSet);
316}
317
318/// SizeOf - Determine size of delta value in bytes.
319///
320unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
321 if (Form == dwarf::DW_FORM_data4) return 4;
322 return TD->getPointerSize();
323}
324
Bill Wendling88423ee2009-05-15 00:11:17 +0000325#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000326void DIESectionOffset::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000327 O << "Off: ";
328 Label.print(O);
329 O << "-";
330 Section.print(O);
331 O << "-" << IsEH << "-" << UseSet;
332}
333#endif
334
335//===----------------------------------------------------------------------===//
336// DIEDelta Implementation
337//===----------------------------------------------------------------------===//
338
339/// EmitValue - Emit delta value.
340///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000341void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000342 bool IsSmall = Form == dwarf::DW_FORM_data4;
343 D->EmitDifference(LabelHi, LabelLo, IsSmall);
344}
345
346/// SizeOf - Determine size of delta value in bytes.
347///
348unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
349 if (Form == dwarf::DW_FORM_data4) return 4;
350 return TD->getPointerSize();
351}
352
Bill Wendling88423ee2009-05-15 00:11:17 +0000353#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000354void DIEDelta::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000355 O << "Del: ";
356 LabelHi.print(O);
357 O << "-";
358 LabelLo.print(O);
359}
360#endif
361
362//===----------------------------------------------------------------------===//
363// DIEEntry Implementation
364//===----------------------------------------------------------------------===//
365
366/// EmitValue - Emit debug information entry offset.
367///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000368void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000369 D->getAsm()->EmitInt32(Entry->getOffset());
370}
371
Bill Wendling88423ee2009-05-15 00:11:17 +0000372#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000373void DIEEntry::print(raw_ostream &O) {
374 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
Bill Wendling88423ee2009-05-15 00:11:17 +0000375}
376#endif
377
378//===----------------------------------------------------------------------===//
379// DIEBlock Implementation
380//===----------------------------------------------------------------------===//
381
382/// ComputeSize - calculate the size of the block.
383///
384unsigned DIEBlock::ComputeSize(const TargetData *TD) {
385 if (!Size) {
386 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
387 for (unsigned i = 0, N = Values.size(); i < N; ++i)
388 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
389 }
390
391 return Size;
392}
393
394/// EmitValue - Emit block data.
395///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000396void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000397 const AsmPrinter *Asm = D->getAsm();
398 switch (Form) {
399 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
400 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
401 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
Chris Lattner894d75a2010-01-22 23:18:42 +0000402 case dwarf::DW_FORM_block: D->EmitULEB128(Size); break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000403 default: llvm_unreachable("Improper form for block"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000404 }
405
406 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
407 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Chris Lattner0ad9c912010-01-22 22:09:00 +0000408 Asm->O << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000409 Values[i]->EmitValue(D, AbbrevData[i].getForm());
410 }
411}
412
413/// SizeOf - Determine size of block data in bytes.
414///
415unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
416 switch (Form) {
417 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
418 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
419 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Chris Lattneraf76e592009-08-22 20:48:53 +0000420 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
Torok Edwinc23197a2009-07-14 16:55:14 +0000421 default: llvm_unreachable("Improper form for block"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000422 }
423 return 0;
424}
425
Bill Wendling88423ee2009-05-15 00:11:17 +0000426#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000427void DIEBlock::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000428 O << "Blk: ";
429 DIE::print(O, 5);
430}
431#endif