For COFF only: dwarf debug info output a label reference as a section relative item...
[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 #include "llvm/Support/MD5.h"
28 using namespace llvm;
29
30 //===----------------------------------------------------------------------===//
31 // DIEAbbrevData Implementation
32 //===----------------------------------------------------------------------===//
33
34 /// Profile - Used to gather unique data for the abbreviation folding set.
35 ///
36 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
37   ID.AddInteger(Attribute);
38   ID.AddInteger(Form);
39 }
40
41 //===----------------------------------------------------------------------===//
42 // DIEAbbrev Implementation
43 //===----------------------------------------------------------------------===//
44
45 /// Profile - Used to gather unique data for the abbreviation folding set.
46 ///
47 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
48   ID.AddInteger(Tag);
49   ID.AddInteger(ChildrenFlag);
50
51   // For each attribute description.
52   for (unsigned i = 0, N = Data.size(); i < N; ++i)
53     Data[i].Profile(ID);
54 }
55
56 /// Emit - Print the abbreviation using the specified asm printer.
57 ///
58 void DIEAbbrev::Emit(AsmPrinter *AP) const {
59   // Emit its Dwarf tag type.
60   AP->EmitULEB128(Tag, dwarf::TagString(Tag));
61
62   // Emit whether it has children DIEs.
63   AP->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     AP->EmitULEB128(AttrData.getAttribute(),
71                     dwarf::AttributeString(AttrData.getAttribute()));
72
73     // Emit form type.
74     AP->EmitULEB128(AttrData.getForm(),
75                     dwarf::FormEncodingString(AttrData.getForm()));
76   }
77
78   // Mark end of abbreviation.
79   AP->EmitULEB128(0, "EOM(1)");
80   AP->EmitULEB128(0, "EOM(2)");
81 }
82
83 #ifndef NDEBUG
84 void DIEAbbrev::print(raw_ostream &O) {
85   O << "Abbreviation @"
86     << format("0x%lx", (long)(intptr_t)this)
87     << "  "
88     << dwarf::TagString(Tag)
89     << " "
90     << dwarf::ChildrenString(ChildrenFlag)
91     << '\n';
92
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())
98       << '\n';
99   }
100 }
101 void DIEAbbrev::dump() { print(dbgs()); }
102 #endif
103
104 //===----------------------------------------------------------------------===//
105 // DIE Implementation
106 //===----------------------------------------------------------------------===//
107
108 DIE::~DIE() {
109   for (unsigned i = 0, N = Children.size(); i < N; ++i)
110     delete Children[i];
111 }
112
113 /// Climb up the parent chain to get the compile unit DIE to which this DIE
114 /// belongs.
115 DIE *DIE::getCompileUnit() {
116   DIE *p = this;
117   while (p) {
118     if (p->getTag() == dwarf::DW_TAG_compile_unit)
119       return p;
120     p = p->getParent();
121   }
122   llvm_unreachable("We should not have orphaned DIEs.");
123 }
124
125 DIEValue *DIE::findAttribute(uint16_t Attribute) {
126   const SmallVectorImpl<DIEValue *> &Values = getValues();
127   const DIEAbbrev &Abbrevs = getAbbrev();
128
129   // Iterate through all the attributes until we find the one we're
130   // looking for, if we can't find it return NULL.
131   for (size_t i = 0; i < Values.size(); ++i)
132     if (Abbrevs.getData()[i].getAttribute() == Attribute)
133       return Values[i];
134   return NULL;
135 }
136
137 #ifndef NDEBUG
138 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
139   const std::string Indent(IndentCount, ' ');
140   bool isBlock = Abbrev.getTag() == 0;
141
142   if (!isBlock) {
143     O << Indent
144       << "Die: "
145       << format("0x%lx", (long)(intptr_t)this)
146       << ", Offset: " << Offset
147       << ", Size: " << Size << "\n";
148
149     O << Indent
150       << dwarf::TagString(Abbrev.getTag())
151       << " "
152       << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
153   } else {
154     O << "Size: " << Size << "\n";
155   }
156
157   const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
158
159   IndentCount += 2;
160   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
161     O << Indent;
162
163     if (!isBlock)
164       O << dwarf::AttributeString(Data[i].getAttribute());
165     else
166       O << "Blk[" << i << "]";
167
168     O <<  "  "
169       << dwarf::FormEncodingString(Data[i].getForm())
170       << " ";
171     Values[i]->print(O);
172     O << "\n";
173   }
174   IndentCount -= 2;
175
176   for (unsigned j = 0, M = Children.size(); j < M; ++j) {
177     Children[j]->print(O, IndentCount+4);
178   }
179
180   if (!isBlock) O << "\n";
181 }
182
183 void DIE::dump() {
184   print(dbgs());
185 }
186 #endif
187
188 void DIEValue::anchor() { }
189
190 #ifndef NDEBUG
191 void DIEValue::dump() const {
192   print(dbgs());
193 }
194 #endif
195
196 //===----------------------------------------------------------------------===//
197 // DIEInteger Implementation
198 //===----------------------------------------------------------------------===//
199
200 /// EmitValue - Emit integer of appropriate size.
201 ///
202 void DIEInteger::EmitValue(AsmPrinter *Asm, uint16_t Form) const {
203   unsigned Size = ~0U;
204   switch (Form) {
205   case dwarf::DW_FORM_flag_present:
206     // Emit something to keep the lines and comments in sync.
207     // FIXME: Is there a better way to do this?
208     if (Asm->OutStreamer.hasRawTextSupport())
209       Asm->OutStreamer.EmitRawText(StringRef(""));
210     return;
211   case dwarf::DW_FORM_flag:  // Fall thru
212   case dwarf::DW_FORM_ref1:  // Fall thru
213   case dwarf::DW_FORM_data1: Size = 1; break;
214   case dwarf::DW_FORM_ref2:  // Fall thru
215   case dwarf::DW_FORM_data2: Size = 2; break;
216   case dwarf::DW_FORM_sec_offset: // Fall thru
217   case dwarf::DW_FORM_ref4:  // Fall thru
218   case dwarf::DW_FORM_data4: Size = 4; break;
219   case dwarf::DW_FORM_ref8:  // Fall thru
220   case dwarf::DW_FORM_data8: Size = 8; break;
221   case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
222   case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
223   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
224   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
225   case dwarf::DW_FORM_addr:
226     Size = Asm->getDataLayout().getPointerSize(); break;
227   default: llvm_unreachable("DIE Value form not supported yet");
228   }
229   Asm->OutStreamer.EmitIntValue(Integer, Size);
230 }
231
232 /// SizeOf - Determine size of integer value in bytes.
233 ///
234 unsigned DIEInteger::SizeOf(AsmPrinter *AP, uint16_t Form) const {
235   switch (Form) {
236   case dwarf::DW_FORM_flag_present: return 0;
237   case dwarf::DW_FORM_flag:  // Fall thru
238   case dwarf::DW_FORM_ref1:  // Fall thru
239   case dwarf::DW_FORM_data1: return sizeof(int8_t);
240   case dwarf::DW_FORM_ref2:  // Fall thru
241   case dwarf::DW_FORM_data2: return sizeof(int16_t);
242   case dwarf::DW_FORM_sec_offset: // Fall thru
243   case dwarf::DW_FORM_ref4:  // Fall thru
244   case dwarf::DW_FORM_data4: return sizeof(int32_t);
245   case dwarf::DW_FORM_ref8:  // Fall thru
246   case dwarf::DW_FORM_data8: return sizeof(int64_t);
247   case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
248   case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
249   case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
250   case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
251   case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
252   default: llvm_unreachable("DIE Value form not supported yet");
253   }
254 }
255
256 #ifndef NDEBUG
257 void DIEInteger::print(raw_ostream &O) const {
258   O << "Int: " << (int64_t)Integer << "  0x";
259   O.write_hex(Integer);
260 }
261 #endif
262
263 //===----------------------------------------------------------------------===//
264 // DIEExpr Implementation
265 //===----------------------------------------------------------------------===//
266
267 /// EmitValue - Emit expression value.
268 ///
269 void DIEExpr::EmitValue(AsmPrinter *AP, uint16_t Form) const {
270   AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
271 }
272
273 /// SizeOf - Determine size of expression value in bytes.
274 ///
275 unsigned DIEExpr::SizeOf(AsmPrinter *AP, uint16_t Form) const {
276   if (Form == dwarf::DW_FORM_data4) return 4;
277   if (Form == dwarf::DW_FORM_sec_offset) return 4;
278   if (Form == dwarf::DW_FORM_strp) return 4;
279   return AP->getDataLayout().getPointerSize();
280 }
281
282 #ifndef NDEBUG
283 void DIEExpr::print(raw_ostream &O) const {
284   O << "Expr: ";
285   Expr->print(O);
286 }
287 #endif
288
289 //===----------------------------------------------------------------------===//
290 // DIELabel Implementation
291 //===----------------------------------------------------------------------===//
292
293 /// EmitValue - Emit label value.
294 ///
295 void DIELabel::EmitValue(AsmPrinter *AP, uint16_t Form) const {
296   AP->EmitLabelReference(Label, SizeOf(AP, Form), Form == dwarf::DW_FORM_strp 
297     || Form == dwarf::DW_FORM_sec_offset || Form == dwarf::DW_OP_call_ref
298     || Form == dwarf::DW_FORM_ref_addr);
299 }
300
301 /// SizeOf - Determine size of label value in bytes.
302 ///
303 unsigned DIELabel::SizeOf(AsmPrinter *AP, uint16_t Form) const {
304   if (Form == dwarf::DW_FORM_data4) return 4;
305   if (Form == dwarf::DW_FORM_sec_offset) return 4;
306   if (Form == dwarf::DW_FORM_strp) return 4;
307   return AP->getDataLayout().getPointerSize();
308 }
309
310 #ifndef NDEBUG
311 void DIELabel::print(raw_ostream &O) const {
312   O << "Lbl: " << Label->getName();
313 }
314 #endif
315
316 //===----------------------------------------------------------------------===//
317 // DIEDelta Implementation
318 //===----------------------------------------------------------------------===//
319
320 /// EmitValue - Emit delta value.
321 ///
322 void DIEDelta::EmitValue(AsmPrinter *AP, uint16_t Form) const {
323   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
324 }
325
326 /// SizeOf - Determine size of delta value in bytes.
327 ///
328 unsigned DIEDelta::SizeOf(AsmPrinter *AP, uint16_t Form) const {
329   if (Form == dwarf::DW_FORM_data4) return 4;
330   if (Form == dwarf::DW_FORM_strp) return 4;
331   return AP->getDataLayout().getPointerSize();
332 }
333
334 #ifndef NDEBUG
335 void DIEDelta::print(raw_ostream &O) const {
336   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
337 }
338 #endif
339
340 //===----------------------------------------------------------------------===//
341 // DIEString Implementation
342 //===----------------------------------------------------------------------===//
343
344 /// EmitValue - Emit string value.
345 ///
346 void DIEString::EmitValue(AsmPrinter *AP, uint16_t Form) const {
347   Access->EmitValue(AP, Form);
348 }
349
350 /// SizeOf - Determine size of delta value in bytes.
351 ///
352 unsigned DIEString::SizeOf(AsmPrinter *AP, uint16_t Form) const {
353   return Access->SizeOf(AP, Form);
354 }
355
356 #ifndef NDEBUG
357 void DIEString::print(raw_ostream &O) const {
358   O << "String: " << Str << "\tSymbol: ";
359   Access->print(O);
360 }
361 #endif
362
363 //===----------------------------------------------------------------------===//
364 // DIEEntry Implementation
365 //===----------------------------------------------------------------------===//
366
367 /// EmitValue - Emit debug information entry offset.
368 ///
369 void DIEEntry::EmitValue(AsmPrinter *AP, uint16_t Form) const {
370   AP->EmitInt32(Entry->getOffset());
371 }
372
373 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
374   // DWARF4: References that use the attribute form DW_FORM_ref_addr are
375   // specified to be four bytes in the DWARF 32-bit format and eight bytes
376   // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
377   // references have the same size as an address on the target system.
378   if (AP->getDwarfDebug()->getDwarfVersion() == 2)
379     return AP->getDataLayout().getPointerSize();
380   return sizeof(int32_t);
381 }
382
383 #ifndef NDEBUG
384 void DIEEntry::print(raw_ostream &O) const {
385   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
386 }
387 #endif
388
389 //===----------------------------------------------------------------------===//
390 // DIEBlock Implementation
391 //===----------------------------------------------------------------------===//
392
393 /// ComputeSize - calculate the size of the block.
394 ///
395 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
396   if (!Size) {
397     const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
398     for (unsigned i = 0, N = Values.size(); i < N; ++i)
399       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
400   }
401
402   return Size;
403 }
404
405 /// EmitValue - Emit block data.
406 ///
407 void DIEBlock::EmitValue(AsmPrinter *Asm, uint16_t Form) const {
408   switch (Form) {
409   default: llvm_unreachable("Improper form for block");
410   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
411   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
412   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
413   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
414   }
415
416   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
417   for (unsigned i = 0, N = Values.size(); i < N; ++i)
418     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
419 }
420
421 /// SizeOf - Determine size of block data in bytes.
422 ///
423 unsigned DIEBlock::SizeOf(AsmPrinter *AP, uint16_t Form) const {
424   switch (Form) {
425   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
426   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
427   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
428   case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
429   default: llvm_unreachable("Improper form for block");
430   }
431 }
432
433 #ifndef NDEBUG
434 void DIEBlock::print(raw_ostream &O) const {
435   O << "Blk: ";
436   DIE::print(O, 5);
437 }
438 #endif