b9bb4af2b811880411e29b95bf612a002ed308da
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DIE.cpp
1 //===--- 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"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/FormattedStream.h"
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // DIEAbbrevData Implementation
30 //===----------------------------------------------------------------------===//
31
32 /// Profile - Used to gather unique data for the abbreviation folding set.
33 ///
34 void 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 ///
45 void 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 ///
56 void DIEAbbrev::Emit(const DwarfPrinter *DP) const {
57   // Emit its Dwarf tag type.
58   // FIXME: Doing work even in non-asm-verbose runs.
59   DP->EmitULEB128(Tag, dwarf::TagString(Tag));
60
61   // Emit whether it has children DIEs.
62   // FIXME: Doing work even in non-asm-verbose runs.
63   DP->EmitULEB128(ChildrenFlag, 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     // FIXME: Doing work even in non-asm-verbose runs.
71     DP->EmitULEB128(AttrData.getAttribute(),
72                     dwarf::AttributeString(AttrData.getAttribute()));
73
74     // Emit form type.
75     // FIXME: Doing work even in non-asm-verbose runs.
76     DP->EmitULEB128(AttrData.getForm(),
77                     dwarf::FormEncodingString(AttrData.getForm()));
78   }
79
80   // Mark end of abbreviation.
81   DP->EmitULEB128(0, "EOM(1)");
82   DP->EmitULEB128(0, "EOM(2)");
83 }
84
85 #ifndef NDEBUG
86 void DIEAbbrev::print(raw_ostream &O) {
87   O << "Abbreviation @"
88     << format("0x%lx", (long)(intptr_t)this)
89     << "  "
90     << dwarf::TagString(Tag)
91     << " "
92     << dwarf::ChildrenString(ChildrenFlag)
93     << '\n';
94
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())
100       << '\n';
101   }
102 }
103 void DIEAbbrev::dump() { print(dbgs()); }
104 #endif
105
106 //===----------------------------------------------------------------------===//
107 // DIE Implementation
108 //===----------------------------------------------------------------------===//
109
110 DIE::~DIE() {
111   for (unsigned i = 0, N = Children.size(); i < N; ++i)
112     delete Children[i];
113 }
114
115 /// addSiblingOffset - Add a sibling offset field to the front of the DIE.
116 ///
117 void DIE::addSiblingOffset() {
118   DIEInteger *DI = new DIEInteger(0);
119   Values.insert(Values.begin(), DI);
120   Abbrev.AddFirstAttribute(dwarf::DW_AT_sibling, dwarf::DW_FORM_ref4);
121 }
122
123 #ifndef NDEBUG
124 void DIE::print(raw_ostream &O, unsigned IncIndent) {
125   IndentCount += IncIndent;
126   const std::string Indent(IndentCount, ' ');
127   bool isBlock = Abbrev.getTag() == 0;
128
129   if (!isBlock) {
130     O << Indent
131       << "Die: "
132       << format("0x%lx", (long)(intptr_t)this)
133       << ", Offset: " << Offset
134       << ", Size: " << Size << "\n";
135
136     O << Indent
137       << dwarf::TagString(Abbrev.getTag())
138       << " "
139       << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
140   } else {
141     O << "Size: " << Size << "\n";
142   }
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
171 void DIE::dump() {
172   print(dbgs());
173 }
174 #endif
175
176
177 #ifndef NDEBUG
178 void DIEValue::dump() {
179   print(dbgs());
180 }
181 #endif
182
183 //===----------------------------------------------------------------------===//
184 // DIEInteger Implementation
185 //===----------------------------------------------------------------------===//
186
187 /// EmitValue - Emit integer of appropriate size.
188 ///
189 void DIEInteger::EmitValue(DwarfPrinter *D, unsigned Form) const {
190   const AsmPrinter *Asm = D->getAsm();
191   unsigned Size = ~0U;
192   switch (Form) {
193   case dwarf::DW_FORM_flag:  // Fall thru
194   case dwarf::DW_FORM_ref1:  // Fall thru
195   case dwarf::DW_FORM_data1: Size = 1; break;
196   case dwarf::DW_FORM_ref2:  // Fall thru
197   case dwarf::DW_FORM_data2: Size = 2; break;
198   case dwarf::DW_FORM_ref4:  // Fall thru
199   case dwarf::DW_FORM_data4: Size = 4; break;
200   case dwarf::DW_FORM_ref8:  // Fall thru
201   case dwarf::DW_FORM_data8: Size = 8; break;
202   case dwarf::DW_FORM_udata: D->EmitULEB128(Integer); return;
203   case dwarf::DW_FORM_sdata: D->EmitSLEB128(Integer, ""); return;
204   default: llvm_unreachable("DIE Value form not supported yet");
205   }
206   Asm->OutStreamer.EmitIntValue(Integer, Size, 0/*addrspace*/);
207 }
208
209 /// SizeOf - Determine size of integer value in bytes.
210 ///
211 unsigned 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);
222   case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
223   case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
224   default: llvm_unreachable("DIE Value form not supported yet"); break;
225   }
226   return 0;
227 }
228
229 #ifndef NDEBUG
230 void DIEInteger::print(raw_ostream &O) {
231   O << "Int: " << (int64_t)Integer
232     << format("  0x%llx", (unsigned long long)Integer);
233 }
234 #endif
235
236 //===----------------------------------------------------------------------===//
237 // DIEString Implementation
238 //===----------------------------------------------------------------------===//
239
240 /// EmitValue - Emit string value.
241 ///
242 void DIEString::EmitValue(DwarfPrinter *D, unsigned Form) const {
243   D->getAsm()->OutStreamer.EmitBytes(Str, /*addrspace*/0);
244   // Emit nul terminator.
245   D->getAsm()->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0);
246 }
247
248 #ifndef NDEBUG
249 void DIEString::print(raw_ostream &O) {
250   O << "Str: \"" << Str << "\"";
251 }
252 #endif
253
254 //===----------------------------------------------------------------------===//
255 // DIELabel Implementation
256 //===----------------------------------------------------------------------===//
257
258 /// EmitValue - Emit label value.
259 ///
260 void DIELabel::EmitValue(DwarfPrinter *D, unsigned Form) const {
261   bool IsSmall = Form == dwarf::DW_FORM_data4;
262   D->EmitReference(Label, false, IsSmall);
263 }
264
265 /// SizeOf - Determine size of label value in bytes.
266 ///
267 unsigned DIELabel::SizeOf(const TargetData *TD, unsigned Form) const {
268   if (Form == dwarf::DW_FORM_data4) return 4;
269   return TD->getPointerSize();
270 }
271
272 #ifndef NDEBUG
273 void DIELabel::print(raw_ostream &O) {
274   O << "Lbl: " << Label->getName();
275 }
276 #endif
277
278 //===----------------------------------------------------------------------===//
279 // DIESectionOffset Implementation
280 //===----------------------------------------------------------------------===//
281
282 /// EmitValue - Emit delta value.
283 ///
284 void DIESectionOffset::EmitValue(DwarfPrinter *D, unsigned Form) const {
285   bool IsSmall = Form == dwarf::DW_FORM_data4;
286   D->EmitSectionOffset(Label, Section, IsSmall, IsEH);
287 }
288
289 /// SizeOf - Determine size of delta value in bytes.
290 ///
291 unsigned DIESectionOffset::SizeOf(const TargetData *TD, unsigned Form) const {
292   if (Form == dwarf::DW_FORM_data4) return 4;
293   return TD->getPointerSize();
294 }
295
296 #ifndef NDEBUG
297 void DIESectionOffset::print(raw_ostream &O) {
298   O << "Off: " << Label->getName() << "-" << Section->getName()
299     << "-" << IsEH;
300 }
301 #endif
302
303 //===----------------------------------------------------------------------===//
304 // DIEDelta Implementation
305 //===----------------------------------------------------------------------===//
306
307 /// EmitValue - Emit delta value.
308 ///
309 void DIEDelta::EmitValue(DwarfPrinter *D, unsigned Form) const {
310   bool IsSmall = Form == dwarf::DW_FORM_data4;
311   D->EmitDifference(LabelHi, LabelLo, IsSmall);
312 }
313
314 /// SizeOf - Determine size of delta value in bytes.
315 ///
316 unsigned DIEDelta::SizeOf(const TargetData *TD, unsigned Form) const {
317   if (Form == dwarf::DW_FORM_data4) return 4;
318   return TD->getPointerSize();
319 }
320
321 #ifndef NDEBUG
322 void DIEDelta::print(raw_ostream &O) {
323   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
324 }
325 #endif
326
327 //===----------------------------------------------------------------------===//
328 // DIEEntry Implementation
329 //===----------------------------------------------------------------------===//
330
331 /// EmitValue - Emit debug information entry offset.
332 ///
333 void DIEEntry::EmitValue(DwarfPrinter *D, unsigned Form) const {
334   D->getAsm()->EmitInt32(Entry->getOffset());
335 }
336
337 #ifndef NDEBUG
338 void DIEEntry::print(raw_ostream &O) {
339   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
340 }
341 #endif
342
343 //===----------------------------------------------------------------------===//
344 // DIEBlock Implementation
345 //===----------------------------------------------------------------------===//
346
347 /// ComputeSize - calculate the size of the block.
348 ///
349 unsigned DIEBlock::ComputeSize(const TargetData *TD) {
350   if (!Size) {
351     const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
352     for (unsigned i = 0, N = Values.size(); i < N; ++i)
353       Size += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
354   }
355
356   return Size;
357 }
358
359 /// EmitValue - Emit block data.
360 ///
361 void DIEBlock::EmitValue(DwarfPrinter *D, unsigned Form) const {
362   const AsmPrinter *Asm = D->getAsm();
363   switch (Form) {
364   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);         break;
365   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);        break;
366   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);        break;
367   case dwarf::DW_FORM_block:  D->EmitULEB128(Size);        break;
368   default: llvm_unreachable("Improper form for block");    break;
369   }
370
371   const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
372   for (unsigned i = 0, N = Values.size(); i < N; ++i)
373     Values[i]->EmitValue(D, AbbrevData[i].getForm());
374 }
375
376 /// SizeOf - Determine size of block data in bytes.
377 ///
378 unsigned DIEBlock::SizeOf(const TargetData *TD, unsigned Form) const {
379   switch (Form) {
380   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
381   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
382   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
383   case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size);
384   default: llvm_unreachable("Improper form for block"); break;
385   }
386   return 0;
387 }
388
389 #ifndef NDEBUG
390 void DIEBlock::print(raw_ostream &O) {
391   O << "Blk: ";
392   DIE::print(O, 5);
393 }
394 #endif