Debug Info: use module flag to set up Dwarf version.
[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 "DwarfDebug.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormattedStream.h"
27 using namespace llvm;
28
29 //===----------------------------------------------------------------------===//
30 // DIEAbbrevData Implementation
31 //===----------------------------------------------------------------------===//
32
33 /// Profile - Used to gather unique data for the abbreviation folding set.
34 ///
35 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
36   ID.AddInteger(Attribute);
37   ID.AddInteger(Form);
38 }
39
40 //===----------------------------------------------------------------------===//
41 // DIEAbbrev Implementation
42 //===----------------------------------------------------------------------===//
43
44 /// Profile - Used to gather unique data for the abbreviation folding set.
45 ///
46 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
47   ID.AddInteger(Tag);
48   ID.AddInteger(ChildrenFlag);
49
50   // For each attribute description.
51   for (unsigned i = 0, N = Data.size(); i < N; ++i)
52     Data[i].Profile(ID);
53 }
54
55 /// Emit - Print the abbreviation using the specified asm printer.
56 ///
57 void DIEAbbrev::Emit(AsmPrinter *AP) const {
58   // Emit its Dwarf tag type.
59   AP->EmitULEB128(Tag, dwarf::TagString(Tag));
60
61   // Emit whether it has children DIEs.
62   AP->EmitULEB128(ChildrenFlag, 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     AP->EmitULEB128(AttrData.getAttribute(),
70                     dwarf::AttributeString(AttrData.getAttribute()));
71
72     // Emit form type.
73     AP->EmitULEB128(AttrData.getForm(),
74                     dwarf::FormEncodingString(AttrData.getForm()));
75   }
76
77   // Mark end of abbreviation.
78   AP->EmitULEB128(0, "EOM(1)");
79   AP->EmitULEB128(0, "EOM(2)");
80 }
81
82 #ifndef NDEBUG
83 void DIEAbbrev::print(raw_ostream &O) {
84   O << "Abbreviation @"
85     << format("0x%lx", (long)(intptr_t)this)
86     << "  "
87     << dwarf::TagString(Tag)
88     << " "
89     << dwarf::ChildrenString(ChildrenFlag)
90     << '\n';
91
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())
97       << '\n';
98   }
99 }
100 void DIEAbbrev::dump() { print(dbgs()); }
101 #endif
102
103 //===----------------------------------------------------------------------===//
104 // DIE Implementation
105 //===----------------------------------------------------------------------===//
106
107 DIE::~DIE() {
108   for (unsigned i = 0, N = Children.size(); i < N; ++i)
109     delete Children[i];
110 }
111
112 /// Climb up the parent chain to get the compile unit DIE to which this DIE
113 /// belongs.
114 DIE *DIE::getCompileUnit() {
115   DIE *p = this;
116   while (p) {
117     if (p->getTag() == dwarf::DW_TAG_compile_unit)
118       return p;
119     p = p->getParent();
120   }
121   llvm_unreachable("We should not have orphaned DIEs.");
122 }
123
124 #ifndef NDEBUG
125 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
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 SmallVectorImpl<DIEAbbrevData> &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, IndentCount+4);
165   }
166
167   if (!isBlock) O << "\n";
168 }
169
170 void DIE::dump() {
171   print(dbgs());
172 }
173 #endif
174
175 void DIEValue::anchor() { }
176
177 #ifndef NDEBUG
178 void DIEValue::dump() const {
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(AsmPrinter *Asm, unsigned Form) const {
190   unsigned Size = ~0U;
191   switch (Form) {
192   case dwarf::DW_FORM_flag_present:
193     // Emit something to keep the lines and comments in sync.
194     // FIXME: Is there a better way to do this?
195     if (Asm->OutStreamer.hasRawTextSupport())
196       Asm->OutStreamer.EmitRawText(StringRef(""));
197     return;
198   case dwarf::DW_FORM_flag:  // Fall thru
199   case dwarf::DW_FORM_ref1:  // Fall thru
200   case dwarf::DW_FORM_data1: Size = 1; break;
201   case dwarf::DW_FORM_ref2:  // Fall thru
202   case dwarf::DW_FORM_data2: Size = 2; break;
203   case dwarf::DW_FORM_sec_offset: // Fall thru
204   case dwarf::DW_FORM_ref4:  // Fall thru
205   case dwarf::DW_FORM_data4: Size = 4; break;
206   case dwarf::DW_FORM_ref8:  // Fall thru
207   case dwarf::DW_FORM_data8: Size = 8; break;
208   case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
209   case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
210   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
211   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
212   case dwarf::DW_FORM_addr:
213     Size = Asm->getDataLayout().getPointerSize(); break;
214   default: llvm_unreachable("DIE Value form not supported yet");
215   }
216   Asm->OutStreamer.EmitIntValue(Integer, Size);
217 }
218
219 /// SizeOf - Determine size of integer value in bytes.
220 ///
221 unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
222   switch (Form) {
223   case dwarf::DW_FORM_flag_present: return 0;
224   case dwarf::DW_FORM_flag:  // Fall thru
225   case dwarf::DW_FORM_ref1:  // Fall thru
226   case dwarf::DW_FORM_data1: return sizeof(int8_t);
227   case dwarf::DW_FORM_ref2:  // Fall thru
228   case dwarf::DW_FORM_data2: return sizeof(int16_t);
229   case dwarf::DW_FORM_sec_offset: // Fall thru
230   case dwarf::DW_FORM_ref4:  // Fall thru
231   case dwarf::DW_FORM_data4: return sizeof(int32_t);
232   case dwarf::DW_FORM_ref8:  // Fall thru
233   case dwarf::DW_FORM_data8: return sizeof(int64_t);
234   case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
235   case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
236   case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
237   case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
238   case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
239   default: llvm_unreachable("DIE Value form not supported yet");
240   }
241 }
242
243 #ifndef NDEBUG
244 void DIEInteger::print(raw_ostream &O) const {
245   O << "Int: " << (int64_t)Integer << "  0x";
246   O.write_hex(Integer);
247 }
248 #endif
249
250 //===----------------------------------------------------------------------===//
251 // DIEExpr Implementation
252 //===----------------------------------------------------------------------===//
253
254 /// EmitValue - Emit expression value.
255 ///
256 void DIEExpr::EmitValue(AsmPrinter *AP, unsigned Form) const {
257   AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
258 }
259
260 /// SizeOf - Determine size of expression value in bytes.
261 ///
262 unsigned DIEExpr::SizeOf(AsmPrinter *AP, unsigned Form) const {
263   if (Form == dwarf::DW_FORM_data4) return 4;
264   if (Form == dwarf::DW_FORM_sec_offset) return 4;
265   if (Form == dwarf::DW_FORM_strp) return 4;
266   return AP->getDataLayout().getPointerSize();
267 }
268
269 #ifndef NDEBUG
270 void DIEExpr::print(raw_ostream &O) const {
271   O << "Expr: ";
272   Expr->print(O);
273 }
274 #endif
275
276 //===----------------------------------------------------------------------===//
277 // DIELabel Implementation
278 //===----------------------------------------------------------------------===//
279
280 /// EmitValue - Emit label value.
281 ///
282 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
283   AP->EmitLabelReference(Label, SizeOf(AP, Form));
284 }
285
286 /// SizeOf - Determine size of label value in bytes.
287 ///
288 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
289   if (Form == dwarf::DW_FORM_data4) return 4;
290   if (Form == dwarf::DW_FORM_sec_offset) return 4;
291   if (Form == dwarf::DW_FORM_strp) return 4;
292   return AP->getDataLayout().getPointerSize();
293 }
294
295 #ifndef NDEBUG
296 void DIELabel::print(raw_ostream &O) const {
297   O << "Lbl: " << Label->getName();
298 }
299 #endif
300
301 //===----------------------------------------------------------------------===//
302 // DIEDelta Implementation
303 //===----------------------------------------------------------------------===//
304
305 /// EmitValue - Emit delta value.
306 ///
307 void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
308   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
309 }
310
311 /// SizeOf - Determine size of delta value in bytes.
312 ///
313 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
314   if (Form == dwarf::DW_FORM_data4) return 4;
315   if (Form == dwarf::DW_FORM_strp) return 4;
316   return AP->getDataLayout().getPointerSize();
317 }
318
319 #ifndef NDEBUG
320 void DIEDelta::print(raw_ostream &O) const {
321   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
322 }
323 #endif
324
325 //===----------------------------------------------------------------------===//
326 // DIEEntry Implementation
327 //===----------------------------------------------------------------------===//
328
329 /// EmitValue - Emit debug information entry offset.
330 ///
331 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
332   AP->EmitInt32(Entry->getOffset());
333 }
334
335 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
336   // DWARF4: References that use the attribute form DW_FORM_ref_addr are
337   // specified to be four bytes in the DWARF 32-bit format and eight bytes
338   // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
339   // references have the same size as an address on the target system.
340   if (AP->getDwarfDebug()->getDwarfVersion() == 2)
341     return AP->getDataLayout().getPointerSize();
342   return sizeof(int32_t);
343 }
344
345 #ifndef NDEBUG
346 void DIEEntry::print(raw_ostream &O) const {
347   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
348 }
349 #endif
350
351 //===----------------------------------------------------------------------===//
352 // DIEBlock Implementation
353 //===----------------------------------------------------------------------===//
354
355 /// ComputeSize - calculate the size of the block.
356 ///
357 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
358   if (!Size) {
359     const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
360     for (unsigned i = 0, N = Values.size(); i < N; ++i)
361       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
362   }
363
364   return Size;
365 }
366
367 /// EmitValue - Emit block data.
368 ///
369 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
370   switch (Form) {
371   default: llvm_unreachable("Improper form for block");
372   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
373   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
374   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
375   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
376   }
377
378   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
379   for (unsigned i = 0, N = Values.size(); i < N; ++i)
380     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
381 }
382
383 /// SizeOf - Determine size of block data in bytes.
384 ///
385 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
386   switch (Form) {
387   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
388   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
389   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
390   case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
391   default: llvm_unreachable("Improper form for block");
392   }
393 }
394
395 #ifndef NDEBUG
396 void DIEBlock::print(raw_ostream &O) const {
397   O << "Blk: ";
398   DIE::print(O, 5);
399 }
400 #endif