[DebugInfo] Introduce DIEExpr variant of DIEValue to hold MCExpr values
[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 "llvm/ADT/Twine.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/Allocator.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(AsmPrinter *AP) const {
57   // Emit its Dwarf tag type.
58   AP->EmitULEB128(Tag, dwarf::TagString(Tag));
59
60   // Emit whether it has children DIEs.
61   AP->EmitULEB128(ChildrenFlag, dwarf::ChildrenString(ChildrenFlag));
62
63   // For each attribute description.
64   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
65     const DIEAbbrevData &AttrData = Data[i];
66
67     // Emit attribute type.
68     AP->EmitULEB128(AttrData.getAttribute(),
69                     dwarf::AttributeString(AttrData.getAttribute()));
70
71     // Emit form type.
72     AP->EmitULEB128(AttrData.getForm(),
73                     dwarf::FormEncodingString(AttrData.getForm()));
74   }
75
76   // Mark end of abbreviation.
77   AP->EmitULEB128(0, "EOM(1)");
78   AP->EmitULEB128(0, "EOM(2)");
79 }
80
81 #ifndef NDEBUG
82 void DIEAbbrev::print(raw_ostream &O) {
83   O << "Abbreviation @"
84     << format("0x%lx", (long)(intptr_t)this)
85     << "  "
86     << dwarf::TagString(Tag)
87     << " "
88     << dwarf::ChildrenString(ChildrenFlag)
89     << '\n';
90
91   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
92     O << "  "
93       << dwarf::AttributeString(Data[i].getAttribute())
94       << "  "
95       << dwarf::FormEncodingString(Data[i].getForm())
96       << '\n';
97   }
98 }
99 void DIEAbbrev::dump() { print(dbgs()); }
100 #endif
101
102 //===----------------------------------------------------------------------===//
103 // DIE Implementation
104 //===----------------------------------------------------------------------===//
105
106 DIE::~DIE() {
107   for (unsigned i = 0, N = Children.size(); i < N; ++i)
108     delete Children[i];
109 }
110
111 /// Climb up the parent chain to get the compile unit DIE to which this DIE
112 /// belongs.
113 DIE *DIE::getCompileUnit() {
114   DIE *p = this;
115   while (p) {
116     if (p->getTag() == dwarf::DW_TAG_compile_unit)
117       return p;
118     p = p->getParent();
119   }
120   llvm_unreachable("We should not have orphaned DIEs.");
121 }
122
123 #ifndef NDEBUG
124 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
125   const std::string Indent(IndentCount, ' ');
126   bool isBlock = Abbrev.getTag() == 0;
127
128   if (!isBlock) {
129     O << Indent
130       << "Die: "
131       << format("0x%lx", (long)(intptr_t)this)
132       << ", Offset: " << Offset
133       << ", Size: " << Size << "\n";
134
135     O << Indent
136       << dwarf::TagString(Abbrev.getTag())
137       << " "
138       << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n";
139   } else {
140     O << "Size: " << Size << "\n";
141   }
142
143   const SmallVectorImpl<DIEAbbrevData> &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, IndentCount+4);
164   }
165
166   if (!isBlock) O << "\n";
167 }
168
169 void DIE::dump() {
170   print(dbgs());
171 }
172 #endif
173
174 void DIEValue::anchor() { }
175
176 #ifndef NDEBUG
177 void DIEValue::dump() const {
178   print(dbgs());
179 }
180 #endif
181
182 //===----------------------------------------------------------------------===//
183 // DIEInteger Implementation
184 //===----------------------------------------------------------------------===//
185
186 /// EmitValue - Emit integer of appropriate size.
187 ///
188 void DIEInteger::EmitValue(AsmPrinter *Asm, unsigned Form) const {
189   unsigned Size = ~0U;
190   switch (Form) {
191   case dwarf::DW_FORM_flag_present:
192     // Emit something to keep the lines and comments in sync.
193     // FIXME: Is there a better way to do this?
194     if (Asm->OutStreamer.hasRawTextSupport())
195       Asm->OutStreamer.EmitRawText(StringRef(""));
196     return;
197   case dwarf::DW_FORM_flag:  // Fall thru
198   case dwarf::DW_FORM_ref1:  // Fall thru
199   case dwarf::DW_FORM_data1: Size = 1; break;
200   case dwarf::DW_FORM_ref2:  // Fall thru
201   case dwarf::DW_FORM_data2: Size = 2; break;
202   case dwarf::DW_FORM_sec_offset: // Fall thru
203   case dwarf::DW_FORM_ref4:  // Fall thru
204   case dwarf::DW_FORM_data4: Size = 4; break;
205   case dwarf::DW_FORM_ref8:  // Fall thru
206   case dwarf::DW_FORM_data8: Size = 8; break;
207   case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
208   case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
209   case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
210   case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
211   case dwarf::DW_FORM_addr:
212     Size = Asm->getDataLayout().getPointerSize(); break;
213   default: llvm_unreachable("DIE Value form not supported yet");
214   }
215   Asm->OutStreamer.EmitIntValue(Integer, Size);
216 }
217
218 /// SizeOf - Determine size of integer value in bytes.
219 ///
220 unsigned DIEInteger::SizeOf(AsmPrinter *AP, unsigned Form) const {
221   switch (Form) {
222   case dwarf::DW_FORM_flag_present: return 0;
223   case dwarf::DW_FORM_flag:  // Fall thru
224   case dwarf::DW_FORM_ref1:  // Fall thru
225   case dwarf::DW_FORM_data1: return sizeof(int8_t);
226   case dwarf::DW_FORM_ref2:  // Fall thru
227   case dwarf::DW_FORM_data2: return sizeof(int16_t);
228   case dwarf::DW_FORM_sec_offset: // Fall thru
229   case dwarf::DW_FORM_ref4:  // Fall thru
230   case dwarf::DW_FORM_data4: return sizeof(int32_t);
231   case dwarf::DW_FORM_ref8:  // Fall thru
232   case dwarf::DW_FORM_data8: return sizeof(int64_t);
233   case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer);
234   case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer);
235   case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer);
236   case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer);
237   case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
238   default: llvm_unreachable("DIE Value form not supported yet");
239   }
240 }
241
242 #ifndef NDEBUG
243 void DIEInteger::print(raw_ostream &O) const {
244   O << "Int: " << (int64_t)Integer << "  0x";
245   O.write_hex(Integer);
246 }
247 #endif
248
249 //===----------------------------------------------------------------------===//
250 // DIEExpr Implementation
251 //===----------------------------------------------------------------------===//
252
253 /// EmitValue - Emit expression value.
254 ///
255 void DIEExpr::EmitValue(AsmPrinter *AP, unsigned Form) const {
256   AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
257 }
258
259 /// SizeOf - Determine size of expression value in bytes.
260 ///
261 unsigned DIEExpr::SizeOf(AsmPrinter *AP, unsigned Form) const {
262   if (Form == dwarf::DW_FORM_data4) return 4;
263   if (Form == dwarf::DW_FORM_sec_offset) return 4;
264   if (Form == dwarf::DW_FORM_strp) return 4;
265   return AP->getDataLayout().getPointerSize();
266 }
267
268 #ifndef NDEBUG
269 void DIEExpr::print(raw_ostream &O) const {
270   O << "Expr: ";
271   Expr->print(O);
272 }
273 #endif
274
275 //===----------------------------------------------------------------------===//
276 // DIELabel Implementation
277 //===----------------------------------------------------------------------===//
278
279 /// EmitValue - Emit label value.
280 ///
281 void DIELabel::EmitValue(AsmPrinter *AP, unsigned Form) const {
282   AP->EmitLabelReference(Label, SizeOf(AP, Form));
283 }
284
285 /// SizeOf - Determine size of label value in bytes.
286 ///
287 unsigned DIELabel::SizeOf(AsmPrinter *AP, unsigned Form) const {
288   if (Form == dwarf::DW_FORM_data4) return 4;
289   if (Form == dwarf::DW_FORM_sec_offset) return 4;
290   if (Form == dwarf::DW_FORM_strp) return 4;
291   return AP->getDataLayout().getPointerSize();
292 }
293
294 #ifndef NDEBUG
295 void DIELabel::print(raw_ostream &O) const {
296   O << "Lbl: " << Label->getName();
297 }
298 #endif
299
300 //===----------------------------------------------------------------------===//
301 // DIEDelta Implementation
302 //===----------------------------------------------------------------------===//
303
304 /// EmitValue - Emit delta value.
305 ///
306 void DIEDelta::EmitValue(AsmPrinter *AP, unsigned Form) const {
307   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
308 }
309
310 /// SizeOf - Determine size of delta value in bytes.
311 ///
312 unsigned DIEDelta::SizeOf(AsmPrinter *AP, unsigned Form) const {
313   if (Form == dwarf::DW_FORM_data4) return 4;
314   if (Form == dwarf::DW_FORM_strp) return 4;
315   return AP->getDataLayout().getPointerSize();
316 }
317
318 #ifndef NDEBUG
319 void DIEDelta::print(raw_ostream &O) const {
320   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
321 }
322 #endif
323
324 //===----------------------------------------------------------------------===//
325 // DIEEntry Implementation
326 //===----------------------------------------------------------------------===//
327
328 /// EmitValue - Emit debug information entry offset.
329 ///
330 void DIEEntry::EmitValue(AsmPrinter *AP, unsigned Form) const {
331   AP->EmitInt32(Entry->getOffset());
332 }
333
334 #ifndef NDEBUG
335 void DIEEntry::print(raw_ostream &O) const {
336   O << format("Die: 0x%lx", (long)(intptr_t)Entry);
337 }
338 #endif
339
340 //===----------------------------------------------------------------------===//
341 // DIEBlock Implementation
342 //===----------------------------------------------------------------------===//
343
344 /// ComputeSize - calculate the size of the block.
345 ///
346 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) {
347   if (!Size) {
348     const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
349     for (unsigned i = 0, N = Values.size(); i < N; ++i)
350       Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
351   }
352
353   return Size;
354 }
355
356 /// EmitValue - Emit block data.
357 ///
358 void DIEBlock::EmitValue(AsmPrinter *Asm, unsigned Form) const {
359   switch (Form) {
360   default: llvm_unreachable("Improper form for block");
361   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
362   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
363   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
364   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
365   }
366
367   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
368   for (unsigned i = 0, N = Values.size(); i < N; ++i)
369     Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
370 }
371
372 /// SizeOf - Determine size of block data in bytes.
373 ///
374 unsigned DIEBlock::SizeOf(AsmPrinter *AP, unsigned Form) const {
375   switch (Form) {
376   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
377   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
378   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
379   case dwarf::DW_FORM_block:  return Size + MCAsmInfo::getULEB128Size(Size);
380   default: llvm_unreachable("Improper form for block");
381   }
382 }
383
384 #ifndef NDEBUG
385 void DIEBlock::print(raw_ostream &O) const {
386   O << "Blk: ";
387   DIE::print(O, 5);
388 }
389 #endif