blob: a62f14d09cb4b2ec572177c196335fe5e3bee78e [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"
Bill Wendling88423ee2009-05-15 00:11:17 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28// DIEAbbrevData Implementation
29//===----------------------------------------------------------------------===//
30
31/// Profile - Used to gather unique data for the abbreviation folding set.
32///
33void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
34 ID.AddInteger(Attribute);
35 ID.AddInteger(Form);
36}
37
38//===----------------------------------------------------------------------===//
39// DIEAbbrev Implementation
40//===----------------------------------------------------------------------===//
41
42/// Profile - Used to gather unique data for the abbreviation folding set.
43///
44void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
45 ID.AddInteger(Tag);
46 ID.AddInteger(ChildrenFlag);
47
48 // For each attribute description.
49 for (unsigned i = 0, N = Data.size(); i < N; ++i)
50 Data[i].Profile(ID);
51}
52
53/// Emit - Print the abbreviation using the specified asm printer.
54///
55void DIEAbbrev::Emit(const AsmPrinter *Asm) const {
56 // Emit its Dwarf tag type.
57 Asm->EmitULEB128Bytes(Tag);
58 Asm->EOL(dwarf::TagString(Tag));
59
60 // Emit whether it has children DIEs.
61 Asm->EmitULEB128Bytes(ChildrenFlag);
62 Asm->EOL(dwarf::ChildrenString(ChildrenFlag));
63
64 // For each attribute description.
65 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
66 const DIEAbbrevData &AttrData = Data[i];
67
68 // Emit attribute type.
69 Asm->EmitULEB128Bytes(AttrData.getAttribute());
70 Asm->EOL(dwarf::AttributeString(AttrData.getAttribute()));
71
72 // Emit form type.
73 Asm->EmitULEB128Bytes(AttrData.getForm());
74 Asm->EOL(dwarf::FormEncodingString(AttrData.getForm()));
75 }
76
77 // Mark end of abbreviation.
78 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(1)");
79 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(2)");
80}
81
82#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +000083void DIEAbbrev::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +000084 O << "Abbreviation @"
Chris Lattnerb01acfa2009-08-23 01:01:17 +000085 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +000086 << " "
87 << dwarf::TagString(Tag)
88 << " "
89 << dwarf::ChildrenString(ChildrenFlag)
Chris Lattnerb01acfa2009-08-23 01:01:17 +000090 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +000091
92 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
93 O << " "
94 << dwarf::AttributeString(Data[i].getAttribute())
95 << " "
96 << dwarf::FormEncodingString(Data[i].getForm())
Chris Lattnerb01acfa2009-08-23 01:01:17 +000097 << '\n';
Bill Wendling88423ee2009-05-15 00:11:17 +000098 }
99}
David Greene0c8b6e62009-12-24 00:27:55 +0000100void DIEAbbrev::dump() { print(dbgs()); }
Bill Wendling88423ee2009-05-15 00:11:17 +0000101#endif
102
103//===----------------------------------------------------------------------===//
104// DIE Implementation
105//===----------------------------------------------------------------------===//
106
107DIE::~DIE() {
108 for (unsigned i = 0, N = Children.size(); i < N; ++i)
109 delete Children[i];
110}
111
Devang Patel2c4ceb12009-11-21 02:48:08 +0000112/// addSiblingOffset - Add a sibling offset field to the front of the DIE.
Bill Wendling88423ee2009-05-15 00:11:17 +0000113///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000114void DIE::addSiblingOffset() {
Bill Wendling88423ee2009-05-15 00:11:17 +0000115 DIEInteger *DI = new DIEInteger(0);
116 Values.insert(Values.begin(), DI);
117 Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
118}
119
Bill Wendling88423ee2009-05-15 00:11:17 +0000120#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000121void DIE::print(raw_ostream &O, unsigned IncIndent) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000122 IndentCount += IncIndent;
123 const std::string Indent(IndentCount, ' ');
124 bool isBlock = Abbrev.getTag() == 0;
125
126 if (!isBlock) {
127 O << Indent
128 << "Die: "
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000129 << format("0x%lx", (long)(intptr_t)this)
Bill Wendling88423ee2009-05-15 00:11:17 +0000130 << ", Offset: " << Offset
131 << ", Size: " << Size
132 << "\n";
133
134 O << Indent
135 << dwarf::TagString(Abbrev.getTag())
136 << " "
137 << dwarf::ChildrenString(Abbrev.getChildrenFlag());
138 } else {
139 O << "Size: " << Size;
140 }
141 O << "\n";
142
143 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
144
145 IndentCount += 2;
146 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
147 O << Indent;
148
149 if (!isBlock)
150 O << dwarf::AttributeString(Data[i].getAttribute());
151 else
152 O << "Blk[" << i << "]";
153
154 O << " "
155 << dwarf::FormEncodingString(Data[i].getForm())
156 << " ";
157 Values[i]->print(O);
158 O << "\n";
159 }
160 IndentCount -= 2;
161
162 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
163 Children[j]->print(O, 4);
164 }
165
166 if (!isBlock) O << "\n";
167 IndentCount -= IncIndent;
168}
169
170void DIE::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000171 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000172}
173#endif
174
175
176#ifndef NDEBUG
177void DIEValue::dump() {
David Greene0c8b6e62009-12-24 00:27:55 +0000178 print(dbgs());
Bill Wendling88423ee2009-05-15 00:11:17 +0000179}
180#endif
181
182//===----------------------------------------------------------------------===//
183// DIEInteger Implementation
184//===----------------------------------------------------------------------===//
185
186/// EmitValue - Emit integer of appropriate size.
187///
188void DIEInteger::EmitValue(Dwarf *D, unsigned Form) const {
189 const AsmPrinter *Asm = D->getAsm();
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000190 unsigned Size = ~0U;
Bill Wendling88423ee2009-05-15 00:11:17 +0000191 switch (Form) {
192 case dwarf::DW_FORM_flag: // Fall thru
193 case dwarf::DW_FORM_ref1: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000194 case dwarf::DW_FORM_data1: Size = 1; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000195 case dwarf::DW_FORM_ref2: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000196 case dwarf::DW_FORM_data2: Size = 2; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000197 case dwarf::DW_FORM_ref4: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000198 case dwarf::DW_FORM_data4: Size = 4; break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000199 case dwarf::DW_FORM_ref8: // Fall thru
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000200 case dwarf::DW_FORM_data8: Size = 8; break;
201 case dwarf::DW_FORM_udata: Asm->EmitULEB128Bytes(Integer); return;
202 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128Bytes(Integer); return;
Torok Edwinc23197a2009-07-14 16:55:14 +0000203 default: llvm_unreachable("DIE Value form not supported yet");
Bill Wendling88423ee2009-05-15 00:11:17 +0000204 }
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000205 Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
Bill Wendling88423ee2009-05-15 00:11:17 +0000206}
207
208/// SizeOf - Determine size of integer value in bytes.
209///
210unsigned DIEInteger::SizeOf(const TargetData *TD, unsigned Form) const {
211 switch (Form) {
212 case dwarf::DW_FORM_flag: // Fall thru
213 case dwarf::DW_FORM_ref1: // Fall thru
214 case dwarf::DW_FORM_data1: return sizeof(int8_t);
215 case dwarf::DW_FORM_ref2: // Fall thru
216 case dwarf::DW_FORM_data2: return sizeof(int16_t);
217 case dwarf::DW_FORM_ref4: // Fall thru
218 case dwarf::DW_FORM_data4: return sizeof(int32_t);
219 case dwarf::DW_FORM_ref8: // Fall thru
220 case dwarf::DW_FORM_data8: return sizeof(int64_t);
Chris Lattneraf76e592009-08-22 20:48:53 +0000221 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
222 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
Torok Edwinc23197a2009-07-14 16:55:14 +0000223 default: llvm_unreachable("DIE Value form not supported yet"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000224 }
225 return 0;
226}
227
Bill Wendling88423ee2009-05-15 00:11:17 +0000228#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000229void DIEInteger::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000230 O << "Int: " << (int64_t)Integer
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000231 << format(" 0x%llx", (unsigned long long)Integer);
Bill Wendling88423ee2009-05-15 00:11:17 +0000232}
233#endif
234
235//===----------------------------------------------------------------------===//
236// DIEString Implementation
237//===----------------------------------------------------------------------===//
238
239/// EmitValue - Emit string value.
240///
241void DIEString::EmitValue(Dwarf *D, unsigned Form) const {
242 D->getAsm()->EmitString(Str);
243}
244
Bill Wendling88423ee2009-05-15 00:11:17 +0000245#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000246void DIEString::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000247 O << "Str: \"" << Str << "\"";
248}
249#endif
250
251//===----------------------------------------------------------------------===//
252// DIEDwarfLabel Implementation
253//===----------------------------------------------------------------------===//
254
255/// EmitValue - Emit label value.
256///
257void DIEDwarfLabel::EmitValue(Dwarf *D, unsigned Form) const {
258 bool IsSmall = Form == dwarf::DW_FORM_data4;
259 D->EmitReference(Label, false, IsSmall);
260}
261
262/// SizeOf - Determine size of label value in bytes.
263///
264unsigned DIEDwarfLabel::SizeOf(const TargetData *TD, unsigned Form) const {
265 if (Form == dwarf::DW_FORM_data4) return 4;
266 return TD->getPointerSize();
267}
268
Bill Wendling88423ee2009-05-15 00:11:17 +0000269#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000270void DIEDwarfLabel::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000271 O << "Lbl: ";
272 Label.print(O);
273}
274#endif
275
276//===----------------------------------------------------------------------===//
277// DIEObjectLabel Implementation
278//===----------------------------------------------------------------------===//
279
280/// EmitValue - Emit label value.
281///
282void DIEObjectLabel::EmitValue(Dwarf *D, unsigned Form) const {
283 bool IsSmall = Form == dwarf::DW_FORM_data4;
Chris Lattner858431d2010-01-16 18:50:28 +0000284 D->EmitReference(Sym, false, IsSmall);
Bill Wendling88423ee2009-05-15 00:11:17 +0000285}
286
287/// SizeOf - Determine size of label value in bytes.
288///
289unsigned DIEObjectLabel::SizeOf(const TargetData *TD, unsigned Form) const {
290 if (Form == dwarf::DW_FORM_data4) return 4;
291 return TD->getPointerSize();
292}
293
Bill Wendling88423ee2009-05-15 00:11:17 +0000294#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000295void DIEObjectLabel::print(raw_ostream &O) {
Chris Lattner858431d2010-01-16 18:50:28 +0000296 O << "Obj: " << Sym->getName();
Bill Wendling88423ee2009-05-15 00:11:17 +0000297}
298#endif
299
300//===----------------------------------------------------------------------===//
301// DIESectionOffset Implementation
302//===----------------------------------------------------------------------===//
303
304/// EmitValue - Emit delta value.
305///
306void DIESectionOffset::EmitValue(Dwarf *D, unsigned Form) const {
307 bool IsSmall = Form == dwarf::DW_FORM_data4;
308 D->EmitSectionOffset(Label.getTag(), Section.getTag(),
309 Label.getNumber(), Section.getNumber(),
310 IsSmall, IsEH, UseSet);
311}
312
313/// SizeOf - Determine size of delta value in bytes.
314///
315unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
316 if (Form == dwarf::DW_FORM_data4) return 4;
317 return TD->getPointerSize();
318}
319
Bill Wendling88423ee2009-05-15 00:11:17 +0000320#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000321void DIESectionOffset::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000322 O << "Off: ";
323 Label.print(O);
324 O << "-";
325 Section.print(O);
326 O << "-" << IsEH << "-" << UseSet;
327}
328#endif
329
330//===----------------------------------------------------------------------===//
331// DIEDelta Implementation
332//===----------------------------------------------------------------------===//
333
334/// EmitValue - Emit delta value.
335///
336void DIEDelta::EmitValue(Dwarf *D, unsigned Form) const {
337 bool IsSmall = Form == dwarf::DW_FORM_data4;
338 D->EmitDifference(LabelHi, LabelLo, IsSmall);
339}
340
341/// SizeOf - Determine size of delta value in bytes.
342///
343unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
344 if (Form == dwarf::DW_FORM_data4) return 4;
345 return TD->getPointerSize();
346}
347
Bill Wendling88423ee2009-05-15 00:11:17 +0000348#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000349void DIEDelta::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000350 O << "Del: ";
351 LabelHi.print(O);
352 O << "-";
353 LabelLo.print(O);
354}
355#endif
356
357//===----------------------------------------------------------------------===//
358// DIEEntry Implementation
359//===----------------------------------------------------------------------===//
360
361/// EmitValue - Emit debug information entry offset.
362///
363void DIEEntry::EmitValue(Dwarf *D, unsigned Form) const {
364 D->getAsm()->EmitInt32(Entry->getOffset());
365}
366
Bill Wendling88423ee2009-05-15 00:11:17 +0000367#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000368void DIEEntry::print(raw_ostream &O) {
369 O << format("Die: 0x%lx", (long)(intptr_t)Entry);
Bill Wendling88423ee2009-05-15 00:11:17 +0000370}
371#endif
372
373//===----------------------------------------------------------------------===//
374// DIEBlock Implementation
375//===----------------------------------------------------------------------===//
376
377/// ComputeSize - calculate the size of the block.
378///
379unsigned DIEBlock::ComputeSize(const TargetData *TD) {
380 if (!Size) {
381 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
382 for (unsigned i = 0, N = Values.size(); i < N; ++i)
383 Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
384 }
385
386 return Size;
387}
388
389/// EmitValue - Emit block data.
390///
391void DIEBlock::EmitValue(Dwarf *D, unsigned Form) const {
392 const AsmPrinter *Asm = D->getAsm();
393 switch (Form) {
394 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
395 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
396 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
397 case dwarf::DW_FORM_block: Asm->EmitULEB128Bytes(Size); break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000398 default: llvm_unreachable("Improper form for block"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000399 }
400
401 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
402 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
403 Asm->EOL();
404 Values[i]->EmitValue(D, AbbrevData[i].getForm());
405 }
406}
407
408/// SizeOf - Determine size of block data in bytes.
409///
410unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
411 switch (Form) {
412 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
413 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
414 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
Chris Lattneraf76e592009-08-22 20:48:53 +0000415 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
Torok Edwinc23197a2009-07-14 16:55:14 +0000416 default: llvm_unreachable("Improper form for block"); break;
Bill Wendling88423ee2009-05-15 00:11:17 +0000417 }
418 return 0;
419}
420
Bill Wendling88423ee2009-05-15 00:11:17 +0000421#ifndef NDEBUG
Chris Lattnerb01acfa2009-08-23 01:01:17 +0000422void DIEBlock::print(raw_ostream &O) {
Bill Wendling88423ee2009-05-15 00:11:17 +0000423 O << "Blk: ";
424 DIE::print(O, 5);
425}
426#endif