blob: 9b37963d206db715c2f282003a2926b906edb59a [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///
56void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
57 // Emit its Dwarf tag type.
58 Asm->EmitULEB128Bytes(Tag);
59 Asm->EOL(dwarf::TagString(Tag));
60
61 // Emit whether it has children DIEs.
62 Asm->EmitULEB128Bytes(ChildrenFlag);
63 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
64
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.
70 Asm->EmitULEB128Bytes(AttrData.getAttribute());
71 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
72
73 // Emit form type.
74 Asm->EmitULEB128Bytes(AttrData.getForm());
75 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
76 }
77
78 // Mark end of abbreviation.
79 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
80 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
81}
82
83#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +000084void DIEAbbrev::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +000085 O << "Abbreviation @"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000086 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +000087 << " "
88 << dwarf::TagString(Tag)
89 << " "
90 << dwarf::ChildrenString(ChildrenFlag)
Chris Lattnerb01acfa2009-08-23 01:01:17 +000091 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +000092
93 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
94 O << " "
95 << dwarf::AttributeString(Data[i].getAttribute())
96 << " "
97 << dwarf::FormEncodingString(Data[i].getForm())
Chris Lattnerb01acfa2009-08-23 01:01:17 +000098 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +000099 }
100}
David Greene0c8b6e62009-12-24 00:27:55 +0000101void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling88423ee2009-05-15 00:11:17 +0000102#endif
103
104//===----------------------------------------------------------------------===//
105// DIE Implementation
106//===----------------------------------------------------------------------===//
107
108DIE::~DIE() {
109 for (unsigned i = 0, N = Children.size(); i < N; ++i)
110 delete Children[i];
111}
112
Devang Patel2c4ceb12009-11-21 02:48:08 +0000113/// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000114///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000115void DIE::addSiblingOffset() {
Bill Wendling88423ee2009-05-15 00:11:17 +0000116 DIEInteger *DI = new DIEInteger(0);
117 Values.insert(Values.begin(), DI);
118 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
119}
120
Bill Wendling88423ee2009-05-15 00:11:17 +0000121#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000122void DIE::print(raw_ostream &O, unsigned IncIndent) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000123 IndentCount += IncIndent;
124 const std::string Indent(IndentCount, ' ');
125 bool isBlock = Abbrev.getTag() == 0;
126
127 if (!isBlock) {
128 O << Indent
129 << "Die: "
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000130 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +0000131 << ", Offset: " << Offset
132 << ", Size: " << Size
133 << "\n";
134
135 O << Indent
136 << dwarf::TagString(Abbrev.getTag())
137 << " "
138 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
139 } else {
140 O << "Size: " << Size;
141 }
142 O << "\n";
143
144 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
145
146 IndentCount += 2;
147 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
148 O << Indent;
149
150 if (!isBlock)
151 O << dwarf::AttributeString(Data[i].getAttribute());
152 else
153 O << "Blk[" << i << "]";
154
155 O << " "
156 << dwarf::FormEncodingString(Data[i].getForm())
157 << " ";
158 Values[i]->print(O);
159 O << "\n";
160 }
161 IndentCount -= 2;
162
163 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
164 Children[j]->print(O, 4);
165 }
166
167 if (!isBlock) O << "\n";
168 IndentCount -= IncIndent;
169}
170
171void DIE::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000172 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000173}
174#endif
175
176
177#ifndef NDEBUG
178void DIEValue::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000179 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000180}
181#endif
182
183//===----------------------------------------------------------------------===//
184// DIEInteger Implementation
185//===----------------------------------------------------------------------===//
186
187/// EmitValue - Emit integer of appropriate size.
188///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000189void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000190 const AsmPrinter *Asm = D->getAsm();
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000191 unsigned Size = ~0U;
Bill Wendling88423ee2009-05-15 00:11:17 +0000192 switch (Form) {
193 case dwarf::DW_FORM_flag: // Fall thru
194 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000195 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000196 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000197 case dwarf::DW_FORM_data2: Size = 2; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000198 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000199 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000200 case dwarf::DW_FORM_ref8: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000201 case dwarf::DW_FORM_data8: Size = 8; break;
202 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); return;
203 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); return;
Torok Edwinc23197a2009-07-14 16:55:14 +0000204 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling88423ee2009-05-15 00:11:17 +0000205 }
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000206 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
Bill Wendling88423ee2009-05-15 00:11:17 +0000207}
208
209/// SizeOf - Determine size of integer value in bytes.
210///
211unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
212 switch (Form) {
213 case dwarf::DW_FORM_flag: // Fall thru
214 case dwarf::DW_FORM_ref1: // Fall thru
215 case dwarf::DW_FORM_data1: return sizeof(int8_t);
216 case dwarf::DW_FORM_ref2: // Fall thru
217 case dwarf::DW_FORM_data2: return sizeof(int16_t);
218 case dwarf::DW_FORM_ref4: // Fall thru
219 case dwarf::DW_FORM_data4: return sizeof(int32_t);
220 case dwarf::DW_FORM_ref8: // Fall thru
221 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Chris Lattneraf76e592009-08-22 20:48:53 +0000222 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
223 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
Torok Edwinc23197a2009-07-14 16:55:14 +0000224 default: llvm_unreachable("DIE Value form not supported yet"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000225 }
226 return 0;
227}
228
Bill Wendling88423ee2009-05-15 00:11:17 +0000229#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000230void DIEInteger::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000231 O << "Int: " << (int64_t)Integer
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000232 << format(" 0x%llx", (unsigned long long)Integer);
Bill Wendling88423ee2009-05-15 00:11:17 +0000233}
234#endif
235
236//===----------------------------------------------------------------------===//
237// DIEString Implementation
238//===----------------------------------------------------------------------===//
239
240/// EmitValue - Emit string value.
241///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000242void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000243 D->getAsm()->EmitString(Str);
244}
245
Bill Wendling88423ee2009-05-15 00:11:17 +0000246#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000247void DIEString::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000248 O << "Str: \"" << Str << "\"";
249}
250#endif
251
252//===----------------------------------------------------------------------===//
253// DIEDwarfLabel Implementation
254//===----------------------------------------------------------------------===//
255
256/// EmitValue - Emit label value.
257///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000258void DIEDwarfLabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000259 bool IsSmall = Form == dwarf::DW_FORM_data4;
260 D->EmitReference(Label, false, IsSmall);
261}
262
263/// SizeOf - Determine size of label value in bytes.
264///
265unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
266 if (Form == dwarf::DW_FORM_data4) return 4;
267 return TD->getPointerSize();
268}
269
Bill Wendling88423ee2009-05-15 00:11:17 +0000270#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000271void DIEDwarfLabel::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000272 O << "Lbl: ";
273 Label.print(O);
274}
275#endif
276
277//===----------------------------------------------------------------------===//
278// DIEObjectLabel Implementation
279//===----------------------------------------------------------------------===//
280
281/// EmitValue - Emit label value.
282///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000283void DIEObjectLabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000284 bool IsSmall = Form == dwarf::DW_FORM_data4;
Chris Lattner858431d2010-01-16 18:50:28 +0000285 D->EmitReference(Sym, false, IsSmall);
Bill Wendling88423ee2009-05-15 00:11:17 +0000286}
287
288/// SizeOf - Determine size of label value in bytes.
289///
290unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
291 if (Form == dwarf::DW_FORM_data4) return 4;
292 return TD->getPointerSize();
293}
294
Bill Wendling88423ee2009-05-15 00:11:17 +0000295#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000296void DIEObjectLabel::print(raw_ostream &O) {
Chris Lattner858431d2010-01-16 18:50:28 +0000297 O << "Obj: " << Sym->getName();
Bill Wendling88423ee2009-05-15 00:11:17 +0000298}
299#endif
300
301//===----------------------------------------------------------------------===//
302// DIESectionOffset Implementation
303//===----------------------------------------------------------------------===//
304
305/// EmitValue - Emit delta value.
306///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000307void DIESectionOffset::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000308 bool IsSmall = Form == dwarf::DW_FORM_data4;
309 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
310 Label.getNumber(), Section.getNumber(),
311 IsSmall, IsEH, UseSet);
312}
313
314/// SizeOf - Determine size of delta value in bytes.
315///
316unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
317 if (Form == dwarf::DW_FORM_data4) return 4;
318 return TD->getPointerSize();
319}
320
Bill Wendling88423ee2009-05-15 00:11:17 +0000321#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000322void DIESectionOffset::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000323 O << "Off: ";
324 Label.print(O);
325 O << "-";
326 Section.print(O);
327 O << "-" << IsEH << "-" << UseSet;
328}
329#endif
330
331//===----------------------------------------------------------------------===//
332// DIEDelta Implementation
333//===----------------------------------------------------------------------===//
334
335/// EmitValue - Emit delta value.
336///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000337void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000338 bool IsSmall = Form == dwarf::DW_FORM_data4;
339 D->EmitDifference(LabelHi, LabelLo, IsSmall);
340}
341
342/// SizeOf - Determine size of delta value in bytes.
343///
344unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
345 if (Form == dwarf::DW_FORM_data4) return 4;
346 return TD->getPointerSize();
347}
348
Bill Wendling88423ee2009-05-15 00:11:17 +0000349#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000350void DIEDelta::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000351 O << "Del: ";
352 LabelHi.print(O);
353 O << "-";
354 LabelLo.print(O);
355}
356#endif
357
358//===----------------------------------------------------------------------===//
359// DIEEntry Implementation
360//===----------------------------------------------------------------------===//
361
362/// EmitValue - Emit debug information entry offset.
363///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000364void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000365 D->getAsm()->EmitInt32(Entry->getOffset());
366}
367
Bill Wendling88423ee2009-05-15 00:11:17 +0000368#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000369void DIEEntry::print(raw_ostream &O) {
370 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
Bill Wendling88423ee2009-05-15 00:11:17 +0000371}
372#endif
373
374//===----------------------------------------------------------------------===//
375// DIEBlock Implementation
376//===----------------------------------------------------------------------===//
377
378/// ComputeSize - calculate the size of the block.
379///
380unsigned DIEBlock::ComputeSize(const TargetData *TD) {
381 if (!Size) {
382 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
383 for (unsigned i = 0, N = Values.size(); i < N; ++i)
384 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
385 }
386
387 return Size;
388}
389
390/// EmitValue - Emit block data.
391///
Chris Lattner066c9ac2010-01-22 22:23:57 +0000392void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const {
Bill Wendling88423ee2009-05-15 00:11:17 +0000393 const AsmPrinter *Asm = D->getAsm();
394 switch (Form) {
395 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
396 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
397 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
398 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000399 default: llvm_unreachable("Improper form for block"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000400 }
401
402 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
403 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
Chris Lattner0ad9c912010-01-22 22:09:00 +0000404 Asm->O << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +0000405 Values[i]->EmitValue(D, AbbrevData[i].getForm());
406 }
407}
408
409/// SizeOf - Determine size of block data in bytes.
410///
411unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
412 switch (Form) {
413 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
414 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
415 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Chris Lattneraf76e592009-08-22 20:48:53 +0000416 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
Torok Edwinc23197a2009-07-14 16:55:14 +0000417 default: llvm_unreachable("Improper form for block"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000418 }
419 return 0;
420}
421
Bill Wendling88423ee2009-05-15 00:11:17 +0000422#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000423void DIEBlock::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000424 O << "Blk: ";
425 DIE::print(O, 5);
426}
427#endif